Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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 ( #1096942=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re: How to print an array of multidimensional arrays with a for or foreach loop
  • Download Code

Replies are listed 'Best First'.
Re^2: How to print an array of multidimensional arrays with a for or foreach loop
by thanos1983 (Parson) on Aug 11, 2014 at 01:22 UTC

    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.

        Why the "but"? If-then-else also returns the value of "then" or "else". But if-then-else is parsed as a whole statement making it hard to use in an expression.

        use v5.14; # Ternary is convenient to use in an expression... my $value1 = int(rand 2) ? 666 : 999; # If-then-else is less convenient... my $value2 = do { if (int rand 2) { 666 } else { 999 } }; say for $value1, $value2;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1096942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2019-08-18 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (134 votes). Check out past polls.

    Notices?