Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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

by oko1 (Deacon)
on Feb 19, 2012 at 15:42 UTC ( #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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954875]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-02-22 15:23 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (331 votes). Check out past polls.