Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Dereference anonymous AoA

by neilwatson (Priest)
on Aug 08, 2014 at 15:18 UTC ( #1096771=perlquestion: print w/replies, xml ) Need Help??

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

Greetings, This code is producing continuous errors. What have I done wrong?
Use of uninitialized value $i in array element at /home/neil/src/perltest/foo.pl line 25. label kept
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $aref = [ [ 'kept', 82 ], [ 'notkept', 1 ], [ 'repaired', 3 ] ]; say Dumper $aref; foreach my $i ( @{ $aref } ) { say "label ". $aref->[$i][0]; }

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Dereference anonymous AoA
by Solo (Deacon) on Aug 08, 2014 at 15:26 UTC

    $i is an element from @$aref. You're treating it like an index. Try:

    say "label " . $i->[0];
Re: Dereference anonymous AoA
by AnomalousMonk (Bishop) on Aug 08, 2014 at 16:39 UTC

    Two ways:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $aref = [ [ 'kept', 82 ], [ 'notkept', 1 ], [ 'repaired', 3 ], ]; print Dumper $aref; ;; for my $i (0 .. $#$aref) { printf qq{label $aref->[$i][0] }; } print qq{\n ---------}; ;; for my $aref_2nd_level (@$aref) { printf qq{label $aref_2nd_level->[0] }; } " $VAR1 = [ [ 'kept', 82 ], [ 'notkept', 1 ], [ 'repaired', 3 ] ]; label kept label notkept label repaired --------- label kept label notkept label repaired

Re: Dereference anonymous AoA
by ikegami (Patriarch) on Aug 08, 2014 at 21:28 UTC
    my $table = [ [ 'kept', 82 ], [ 'notkept', 1 ], [ 'repaired', 3 ] ]; for my $i ( 0 .. $#{ $table } ) # Iterates over the indexes. { say "label ". $table->[$i][0]; }
    or
    for my $row ( @{ $table } ) # Iterates over the values. { say "label ". $row->[0]; }
Re: Dereference anonymous AoA
by Anonymous Monk on Aug 08, 2014 at 19:41 UTC

    Foreach loop provides list context, so foreach my $i ( @{ $aref } ) explodes into its elements

    foreach my $i( ['kept', 82], ['notkept', 1], ['repaired', 3] ) { ... # so $aref->[$i][0] # looks like $areg->[ ['kept', 82] ][0] #
    Here the first lines out error output:
    $ perl buggy_array.pl 2> out.txt (it hangs here, press Ctrl-C to stop) $ head -6 out.txt Use of reference "ARRAY(0x1cfb148)" as array index at ary.pl line 21. Use of uninitialized value in concatenation (.) or string at ary.pl li +ne 21. Use of reference "ARRAY(0x1d0fad8)" as array index at ary.pl line 21. Use of uninitialized value in concatenation (.) or string at ary.pl li +ne 21. Use of reference "ARRAY(0x1d0fbf8)" as array index at ary.pl line 21. Use of uninitialized value in concatenation (.) or string at ary.pl li +ne 21.
    Now, why does it keep looping?
    perl -E 'my $aref=[undef]; say 0 + $aref' 19468616
    19468616 is the memory address of $aref here. Which means
    $areg->[ ['kept', 82] ][0] # becomes something like # $areg->[ 9999999 ][0] # (or whatever the memory address of that array is...)
    When you do stuff like $aref->[999][0] and element 999 of $aref doesn't exist, Perl will extend $aref. It will fill it with undef, up to element 999.
    perl -E 'my $aref=[undef]; $aref->[1000][0]; say scalar @{ $aref }' 1001
    They call it 'autovivification' (sp?). (but if you do $aref->[999][500], Perl will not create the rightmost array - the one with 500 elements)

    So, now your $aref has tons of of undefs. So the loop keeps looping and issuing warnings.

    So yeah, all in all, that was a pretty interesting question, I must say.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-07-05 19:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?