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

Array of Arrays

by Lhamo_rin (Friar)
on Jun 27, 2003 at 11:04 UTC ( [id://269507]=perlquestion: print w/replies, xml ) Need Help??

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

I'm attempting to do the following:
for (@{$arrays}) { print $_; }
But instead of getting my values I get the following instead:
ARRAY(0x401a04f8) ARRAY(0x401a0528)
and several more like this. Can you please tell me where I am making my error? I am expecting scalar values that are in
@arrays->[0][1]
for example.
Lhamo_rin

Replies are listed 'Best First'.
Re: Array of Arrays
by davorg (Chancellor) on Jun 27, 2003 at 11:36 UTC

    Well it would be easier to help you if we had the code which builds up the data structure. Looking at your code and the results you're getting I'd guess that:

    • $array contains a reference to an array
    • each element of the array referenced in $array is a reference to another array

    The code you want is probably something like this:

    foreach ($@array) { print join ', ', @$_; print "\n"; }

    You should read perldoc perllol for more details on how this works.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Array of Arrays
by benn (Vicar) on Jun 27, 2003 at 11:39 UTC
    As ever, Data::Dumper comes in very handy here in examining stuctures of this type....
    use Data::Dumper; print Dumper($arrays);
    ...would show you that you're dealing with an array of arrays.

    Cheers, Ben.

Re: Array of Arrays
by Tomte (Priest) on Jun 27, 2003 at 11:09 UTC

    An array can only hold scalars, so you have an array of array-refs referenced to by $arrays :-)
    Try

    foreach(@{$arrays}) { print @{$_}; }

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      Hi Tomte,

      To save all the values running into each other, you might want to quote the arrays, like
      print "@$_\n" for @$arrays;
      hope this helps

      thinker
Re: Array of Arrays
by neilwatson (Priest) on Jun 27, 2003 at 13:51 UTC
    I used to have troubles with data structures like this. The perl documentation did not seem clear to me. I wondered how others seemed to find it so easy. After spending alot of time asking questions here I began to understand. Finally, one Christmas, Santa brought me the Camel book. There, nicely laid out, was a great chapter on advanced data structures. If only I'd had that book a few months prior :)

    Neil Watson
    watson-wilson.ca

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://269507]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found