http://www.perlmonks.org?node_id=1096939

thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I came across this problem, that I am having an array with elements and another array inside the array. I have created a minimal sample of code to represent what I mean.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = (['element-1','element-2'],'element-3'); my ($row,$column); print "\n" . Dumper(\@array) . "\n"; print $array[0][0] . "\n"; print $array[0][1] . "\n"; print $array[1] . "\n" . "\n"; foreach $row (@array) { foreach $column (@$row) { print "This is the \$column: ".$column."\n"; } } __END__ $VAR1 = [ [ 'element-1', 'element-2' ], 'element-3' ]; element-1 element-2 element-3 This is the $column: element-1 This is the $column: element-2 Can't use string ("element-3") as an ARRAY ref while "strict refs" in +use at arraysarray.pl line 15.

As a solution to my problem, I found to use all elements as an array. Sample of code and output provided under:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = (['element-1','element-2'],['element-3']); my ($row,$column); print "\n" . Dumper(\@array) . "\n"; print $array[0][0] . "\n"; print $array[0][1] . "\n"; print $array[1][0] . "\n" . "\n"; =comment for($row = 0; $row < scalar @array; $row++) { for($column = 0; $column < 2; $column++) { print $array[$row][$column] . "\n"; } print "\n"; } =cut foreach $row (@array) { foreach $column (@$row) { print "This is the \$column: ".$column."\n"; } } __END__ $VAR1 = [ [ 'element-1', 'element-2' ], [ 'element-3' ] ]; element-1 element-2 element-3 This is the $column: element-1 This is the $column: element-2 This is the $column: element-3

But this is a not a correct solution, there must be another way of printing this elements. Since Data::Dumper does it, there must be another way.

I have spend some time trying to figure it out, or by searching online but I could not found a solid solution, that is why I created my solution, although that I know it is not a good solution it was a solution to my problem.

So out of curiosity, I am asking if anyone has solved this problem earlier.

Thank you in advance for your time and effort reading and replying to my problem.

Seeking for Perl wisdom...on the process of learning...not there...yet!