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

comment on

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

Greetings, fellow monks;

I have a sub that needs to print out an array of arrays (or array of arrayrefs, depending on the version of my code), but for some reason foreach doesn't seem to select anything from the first array.

An example would perhaps clarify the problem more:

sub convFieldToXML($) { my $fldref = shift; print "fldref=\n"; dump($fldref); print "fldref[TXT]=\n"; dump(@$fldref[TXT]); if (ref(@$fldref[TXT]) eq 'ARRAY') { foreach my $tmp (@$fldref[TXT]) { print "tmp=\n"; dump($tmp); } } else { # snip --8<--; } }

Short fragment of output:

fldref= [ 500, 0, 0, [ ["\$a", "Tarina Babarista, pienest{ elefantista"], ["\$s", "(26'34); orkestroitu"], ["\$d", "Poulenc, Francis"], ["\$g", "Brunhoff, Jean de "], ["\$h", "P|ysti, Lasse"], ["\$j", "Fran*8aix, Jean"], ], ] fldref[TXT]= [ ["\$a", "Tarina Babarista, pienest{ elefantista"], ["\$s", "(26'34); orkestroitu"], ["\$d", "Poulenc, Francis"], ["\$g", "Brunhoff, Jean de "], ["\$h", "P|ysti, Lasse"], ["\$j", "Fran*8aix, Jean"], ] tmp= [ ["\$a", "Tarina Babarista, pienest{ elefantista"], ["\$s", "(26'34); orkestroitu"], ["\$d", "Poulenc, Francis"], ["\$g", "Brunhoff, Jean de "], ["\$h", "P|ysti, Lasse"], ["\$j", "Fran*8aix, Jean"], ]

Or also (with refs):

fldref= [ 500, 0, 0, [ \["\$a", "Tarina Babarista, pienest{ elefantista"], \["\$s", "(26'34); orkestroitu"], \["\$d", "Poulenc, Francis"], \["\$g", "Brunhoff, Jean de "], \["\$h", "P|ysti, Lasse"], \["\$j", "Fran*8aix, Jean"], ], ] fldref[TXT]= [ \["\$a", "Tarina Babarista, pienest{ elefantista"], \["\$s", "(26'34); orkestroitu"], \["\$d", "Poulenc, Francis"], \["\$g", "Brunhoff, Jean de "], \["\$h", "P|ysti, Lasse"], \["\$j", "Fran*8aix, Jean"], ] tmp= [ \["\$a", "Tarina Babarista, pienest{ elefantista"], \["\$s", "(26'34); orkestroitu"], \["\$d", "Poulenc, Francis"], \["\$g", "Brunhoff, Jean de "], \["\$h", "P|ysti, Lasse"], \["\$j", "Fran*8aix, Jean"], ]

Note that @$fldref[TXT] has the same output as $tmp, despite the foreach(). I expected $tmp to contain each array from the second level of array-of-arrays (or array-of-refs). Instead, foreach doesn't seem to select anything.

Help?


In reply to foreach doesn't select array element by december

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-05-29 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?