Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Autovivification identification

by Random_Walk (Parson)
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 } ] } ] } };

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!


Comment on Re: Autovivification identification
Select or Download Code
Re^2: Autovivification identification
by choroba (Abbot) 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.

      Cheers,
      R.

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-08-02 08:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls