Wikia

How To Wiki

How to print a 2D array in Perl

1,794pages on
this wiki
Talk0

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 ?? 

Around Wikia's network

Random Wiki