Fandom

How To Wiki

How to print a 2D array in Perl

1,795pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Here is a simple function (subroutine) for printing 2-dimensional (2D) arrays in Perl

FunctionEdit

sub print_2d {
	my @array_2d=@_;
	for(my $i = 0; $i <= $#array_2d; $i++){
	   for(my $j = 0; $j <= $#{$array_2d[0]} ; $j++){
	      print "$array_2d[$i][$j] ";
	   }
	   print "\n";
	}
}

ExampleEdit

  • Code:
my @data = (
[ 1, 5, 2, 19, 3, 7, '?'],
[ 'one', 'five', 'two', 'nineteen', 'three', 'seven', '??' ]
);

print_2d(@data);
  • Output
1 5 2 19 3 7 ? 
one five two nineteen three seven ?? 

Also on Fandom

Random Wiki