Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How can I access the values of an "array" extracted using Range

by oko1 (Deacon)
on Feb 19, 2012 at 15:42 UTC ( [id://954875]=note: print w/replies, xml ) Need Help??


in reply to How can I access the values of an "array" extracted using Range

The problem is that '$array' is not an array; it's an array reference - similar to a pointer. Your code seems fine, all except for that point. Quick overview of dealing with array references:

# Turning an arrayref into an array my @arr = @$aref; # Extracting a single value from an arrayref print ${$aref}[0]; # Or, more readable and less problematic: print $aref -> [0]; # Looping over the pointed-to list for my $i (@$aref){ ... }

In the above case, the structure that you're getting back from 'Range()' is an "AoA" - an array of arrays - which looks like this:

$aref = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];

Notice the second level of indirection, there? The result is that just converting the ref to an array is going to give you a list of... arrayrefs, which need to be expanded into arrays. The solution that netwallah shows is correct - I just wanted to give you a more general heads-up for this kind of problem. So, expanding the above looks like this:

for my $deref (@$aref){ for my $element (@$deref){ print "$element\n"; } }

Again, for more on data structures - AoAs, HoHs, etc. - see perldoc perlref and the excellent Data Structures Cookbook.

-- 
I hate storms, but calms undermine my spirits.
 -- Bernard Moitessier, "The Long Way"

Replies are listed 'Best First'.
Re^2: How can I access the values of an "array" extracted using Range
by ralph (Initiate) on Feb 20, 2012 at 04:16 UTC
    Many thanks oko1--

    Your answer was most helpful. I appreciate the extra time you took to offer some additional details. It made a big difference.

    ralph

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-28 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found