Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to print an array of multidimensional arrays with a for or foreach loop

by thanos1983 (Pilgrim)
on Aug 10, 2014 at 23:25 UTC ( #1096939=perlquestion: print w/ replies, xml ) Need Help??
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!

Comment on How to print an array of multidimensional arrays with a for or foreach loop
Select or Download Code
Re: How to print an array of multidimensional arrays with a for or foreach loop
by Anonymous Monk on Aug 10, 2014 at 23:28 UTC

      Hello Anonymous Monk,

      Thanks, I searched through the links, and I found so many useful answers and possible read ups.

      Thank you for your time and effort.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to print an array of multidimensional arrays with a for or foreach loop
by ikegami (Pope) on Aug 10, 2014 at 23:29 UTC
    for my $row (@array) { for my $col (ref($row) ? @$row : $row) { print $col, "\n"; } }

    This assumes the elements at the same level as the array can't be references.

      Hello ikegami,

      I have seen several times this syntax (ref($row) ? @$row : $row) but I was not aware how it was operating. So I looked it up and I found ternary operator ? it was so simple [condition] ? [true expression] : [false expression].

      Thank you for time and effort.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        It's like if-then-else, but the value of "then" or "else" is returned by the whole.
Re: How to print an array of multidimensional arrays with a for or foreach loop
by Anonymous Monk on Aug 10, 2014 at 23:35 UTC

    Have a look at the ref function:

    foreach $row (@array) { foreach $column (ref $row ? @$row : $row) { print "This is the \$column: ".$column."\n"; } }

    This assumes that if something is a reference, it's an array reference. If $row can also contain hash references or other data, you'll have to look at the return value of ref.

    Also there's CPAN modules, although I haven't used them myself I've sometimes seen them recommended: Data::Walk, Data::Diver, Data::Visitor, Data::Path

      Hello Anonymous Monk,

      So many useful modules, I usually use Data::Dumper but is always nice to learn new things.

      My answer was simple but some times when the user dose not have the knowledge can become so complicated.

      Thank you for your time and effort, reading and replying to my question.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1096939]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2014-10-20 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls