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

Re: Autovivification identification

by Random_Walk (Prior)
on Aug 01, 2013 at 09:35 UTC ( #1047405=note: print w/replies, xml ) Need Help??

in reply to Autovivification identification

Perl knows what you want to do, because that is what you have asked it to do:

# You use push so you must want an array push @{ $provisions{$person} }, $1; # You use hash of hash addressing so you must want a hash $total_bytes{$source}{$destination} += $bytes;

The programmer has made the decision based on the data structure thought to be most useful in each case.

You can make it as crazy as you like and Perl will obey...

perl -MData::Dumper -le'$t{x}{y}[2]{foo}[3]{bar}=1; print Dumper \%t' $VAR1 = { 'x' => { 'y' => [ undef, undef, { 'foo' => [ undef, undef, undef, { 'bar' => 1 } ] } ] } };


Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: Autovivification identification
by choroba (Bishop) on Aug 01, 2013 at 09:42 UTC
    Note that in recent Perl (since 5.14), push can take an array reference as the first argument. This is an experimental feature and no autovivification happens here:
    my $x = []; push $x, 0, 1, 2; # OK push $x->[3], 'ouch'; # Error: array reference expected.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I did not know that. I kinda like it, but I am not sure if it leads to more readable code for those who come after me.


      Pereant, qui ante nos nostra dixerunt!
Re^2: Autovivification identification
by gaurav (Acolyte) on Aug 01, 2013 at 10:18 UTC

    thanks...I was thinking the same..but was little bit confused..because of amateurishness in Perl

      "...amateurishness in Perl" or "amateurishness in the observer?"

      A little knowledge is a dangerous thing; so is failure to indent for readability.

        I am very thank full to you,but want to make one thing clear ,that don't have any disrespect for Perl.My intention was same as yours',means I was calling my self, an amateur

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-04-21 11:55 GMT
Find Nodes?
    Voting Booth?