Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Autovivification identification

by gaurav (Acolyte)
on Aug 01, 2013 at 06:53 UTC ( #1047368=perlquestion: print w/ replies, xml ) Need Help??
gaurav has asked for the wisdom of the Perl Monks concerning the following question:

Hi!! ,I have one query regarding Array-Autovivification & Hash-Autovivification . Please go through the below codes<\p>

Code for Array-Aut

my %provisions; my $person; while (<>) { if (/^(\S.*)/) { # a person's name (no leading whitespace) $person = $1; } elsif (/^\s+(\S.*)/) { # a provision die 'No person yet!' unless defined $person; push @{ $provisions{$person} }, $1; } else { die "I don't understand: $_"; } }

Code for Hash

my %total_bytes; while (<>) { my ($source, $destination, $bytes) = split; $total_bytes{$source}{$destination} += $bytes; } for my $source (sort keys %total_bytes) { for my $destination (sort keys %{ $total_bytes{$source} }) { print "$source => $destination:", " $total_bytes{$source}{$destination} bytes\n"; } print "\n"; }

Now my query is that how Perl will go for array autovivification & hash autovivification in respective codes

for your purpose ,I am also given you the input files for the respective codes

for Array

The Skipper blue_shirt hat jacket preserver sunscreen Professor sunscreen water_bottle slide_rule Gilligan red_shirt hat lucky_socks water_bottle The Skipper sextant

for hash

professor.hut skipper.crew.hut 8655 ginger.girl.hut laser3.copyroom.hut 6404 skipper.crew.hut fileserver.copyroom.hut 8534 gilligan.crew.hut skipper.crew.hut 1004 laser3.copyroom.hut ginger.girl.hut 4366 maryann.girl.hut professor.hut 8454 skipper.crew.hut professor.hut 7475 gilligan.crew.hut maryann.girl.hut 8128 gilligan.crew.hut maryann.girl.hut 5277 gilligan.crew.hut fileserver.copyroom.hut 6117 skipper.crew.hut professor.hut 5101 gilligan.crew.hut gilligan.crew.hut 9973 ginger.girl.hut fileserver.copyroom.hut 1448 skipper.crew.hut gilligan.crew.hut 2146 fileserver.copyroom.hut gilligan.crew.hut 9568 maryann.girl.hut laser3.copyroom.hut 8024 fileserver.copyroom.hut laser3.copyroom.hut 9574

please explain it to me ,that how perl has been identified that it has to go for Array or Hash Autovivification?

Comment on Autovivification identification
Select or Download Code
Re: Autovivification identification
by Loops (Hermit) on Aug 01, 2013 at 07:06 UTC
Re: Autovivification identification
by Random_Walk (Parson) on Aug 01, 2013 at 09:35 UTC

    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!
      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!

      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.
Re: Autovivification identification
by ikegami (Pope) on Aug 02, 2013 at 04:18 UTC

    that how perl has been identified that it has to go for Array or Hash Autovivification?

    Autovivification is the creation of a variable by a dereference when the variable being dereferenced is undefined.

    Perl knows what to create based on the kind of deference used.

    • If you have an array element dereference ($$r[$i], $r->[$i]), it'll create an array.
    • If you have an hash element dereference ($$r{$k}, $r->{$i}), it'll create a hash.
    • If you have an array dereference (@$r), it'll create an array.
    • If you have an hash dereference (%$r), it'll create a hash.
    • etc.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-08-23 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (175 votes), past polls