Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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!

In reply to How to print an array of multidimensional arrays with a for or foreach loop by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2019-11-12 08:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (64 votes). Check out past polls.

      Notices?