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

Dereference array of arrays

by monkini (Initiate)
on Nov 12, 2013 at 14:45 UTC ( [id://1062204]=perlquestion: print w/replies, xml ) Need Help??

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

How can I retrieve values from array of arrays in a loop like that:

my $j=0; while ($j < $#AoA+1) { my @var = shift @AoA; my $a = $var[1]; my $b = $var[2]; }

My data looks like this:

$VAR1 = [ [ '0.93', 'a1', 'b1' ], [ '0.89', 'a2', 'b2' ], [ '0.88', 'a3', 'b3' ], [ '0.87', 'a4', 'b4' ], [ '0.86', 'a5', 'b5' ] ];

So that $a='a1', $b='b1' etc.

Replies are listed 'Best First'.
Re: Dereference array of arrays
by kennethk (Abbot) on Nov 12, 2013 at 15:20 UTC

    A little off-topic, but you should be aware that $a and $b are special variables associated with sort. It's generally considered risky/poor-form to use them outside of that context. Just to help you avoid developing bad habits.

    To answer your original question, you could modify your posted code to get

    while (@AoA) { my $var = shift @AoA; my $a = $var->[1]; my $b = $var->[2]; }
    or
    while (@AoA) { my $var = shift @AoA; my ($a, $b) = @{$var}[1,2]; }
    or
    while (@AoA) { my @var = @{shift @AoA}; my $a = $var[1]; my $b = $var[2]; }
    or
    for my $i (0 .. $#AoA) { my $a = $AoA[$i][1]; my $b = $AoA[$i][2]; }
    or...

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Dereference array of arrays
by 2teez (Vicar) on Nov 12, 2013 at 14:52 UTC

    perldsc

    UPDATE:
    Or you could do like so:

    use warnings; use strict; my $array_ref = [ [ '0.93', 'a1', 'b1' ], [ '0.89', 'a2', 'b2' ], [ '0.88', 'a3', 'b3' ], [ '0.87', 'a4', 'b4' ], [ '0.86', 'a5', 'b5' ] ]; for my $val (@$array_ref) { for ( 1 .. $#$val ) { print $val->[$_], $/; } }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Or even just:

      use warnings; use strict; my $array_ref = [ [ '0.93', 'a1', 'b1' ], [ '0.89', 'a2', 'b2' ], [ '0.88', 'a3', 'b3' ], [ '0.87', 'a4', 'b4' ], [ '0.86', 'a5', 'b5' ] ]; for my $row (@$array_ref) { for my $element (@$row) { print $element, "\n"; # using say, or setting $\ locally would wo +rk as well } }
      You often have no need to know the indexes when looping through an array. And since an array begins with index 0, you would skip the first element with : for ( 1 .. $#$val ) UPDATE: which is done on purpose, my bad \o/

        ..You often have no need to know the indexes when looping through an array. And since an array begins with index 0, you would skip the first element with : for ( 1 .. $#$val )..

        And how was I looping through the array of array provided by the OP? Please check Re: Dereference array of arrays again. OR maybe that is met for the OP.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Dereference array of arrays
by kcott (Archbishop) on Nov 12, 2013 at 18:51 UTC

    G'day monkini,

    Some points on the code you posted:

    • In scalar context, @array_name evaluates to the number of elements in that array. Your condition would have been better as ($j < @AoA): it's easier to read and has one less calculation for every iteration. $#array_name evaluates to the last index in the array: your code suggests you understand this. The condition is actually unnecessary: explained below.
    • A for loop is almost always a better choice for iterating an array. In this instance, the first two lines of code could have been replaced by just: for (@AoA) {
    • shift will modify @AoA and eventually remove all elements from it. Did you want such a modification? Did you consider the extra processing involving (in every iteration) with this modification?
    • $a and $b: the issue here has already been covered by kennethk (above).
    • Rather than using multiple statements each retrieving a single element, use a single statement that retrieves an array slice (see "perldata: Slices").

    Putting all that together, the code you posted could have been written as:

    for (@AoA) { my ($x, $y) = @{$_}[1, 2]; }

    Here's my test:

    #!/usr/bin/env perl -l use strict; use warnings; my @AoA = ([qw{0.93 a1 b1}], [qw{0.89 a2 b2}], [qw{0.88 a3 b3}], [qw{0.87 a4 b4}], [qw{0.86 a5 b5}]); for (@AoA) { my ($x, $y) = @{$_}[1, 2]; # Now do something with the retrieved values, e.g. print "$x $y"; }

    Output:

    a1 b1 a2 b2 a3 b3 a4 b4 a5 b5

    -- Ken

Re: Dereference array of arrays
by ww (Archbishop) on Nov 12, 2013 at 19:22 UTC
    monkini:

    You seem to pose a new and usually very basic question so ofter that the pace that suggests you haven't (and are not) studying Perl in any systematic (nor much of any other) manner.

    And one wonders (well, /me wonders) if you learned anything from answers to one of your questionsw yesterday, Dereferencing array of hashes. It's really not a stretch to read the replies, understand them and the references they offer, and to have thus avoided this thread.

    Try reading "Learning Perl" ( less than USD 2.00 )and the Tutorials here before asking others how to resolve each little glitch in your (minimal) effort to write the code needed to complete a project.

    Surely, you've seen some of the responses to "gimme'" nodes that point out that the Monastery is an institution which aims to help you learn; NOT one that writes your code for you.

      monkini: ... and, if it hasn't already been mentioned, take a look at Modern Perl by chromatic: see his or her monk node for a link to a free download link.

Re: Dereference array of arrays
by Laurent_R (Canon) on Nov 12, 2013 at 19:52 UTC

    Dear monkini,

    I certainly don't want to be rude or hostile in any way toward you, but I have to ask you a few questions. You've posted yesterday a question (Dereferencing array of hashes) on how to dereference an array of hashes and have received a number of valuable answers from various monks including myself. You haven't answered to the various solutions, so I assume that at least one of them did fit the bill. (You could have answered people who tried to help you, saying what it did work for you, for example, or thanking monks for their help, it would have been nice, but that is not my point.) I would hope that you tried to understand the solutions that were suggested. And, if you did try and did understand them, and also made the effort of reading the basic documentation, I would assume that you should be able to dereference an AoA the same way, it is quite similar, or at least you should be able to get very close to the solution. Since you obviously don't seem to, is there anything in the solutions suggested for that other question that you did not understand? Did you try them? Did you try to understand them? Is there anything in them that you don't understand where you need further help?

Log In?
Username:
Password:

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

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

    No recent polls found