Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Occurence List

by Anonymous Monk
on Oct 10, 2012 at 21:27 UTC ( #998323=note: print w/ replies, xml ) Need Help??


in reply to Occurence List

In the above posting, notice how the mere mention of a hash-key causes it to magically appear, with the initial value of zero, so that it can be "++" incremented to the value of one. This bit of perl-mojo is called "autovivification." Otherwise known as "do the right thing quietly and with a minimum of fuss."


Comment on Re: Occurence List
Re^2: Occurence List
by Anonymous Monk on Oct 10, 2012 at 23:10 UTC

    In the above posting, notice how the mere mention of a hash-key causes it to magically appear, with the initial value of zero, so that it can be "++" incremented to the value of one. This bit of perl-mojo is called "autovivification." Otherwise known as "do the right thing quietly and with a minimum of fuss."

    No , that is not autovivification . Just like this is not autovivification

    $foo++; $bar[$_]++;

      However, when you

      use strict;

      writing

      $foo++;

      will cause perl to complain (if you have not declared (and thus created) $foo before),
      while writing

      $bar[396]

      will cause no complaints, even if you never used this index before(or a higher one), or never told Perl how big @bar should be. You will have to declare @bar, of course. So these two examples are not comparable. And I believe the second example actually is where we see autovivication at work.

        will cause perl to complain (if you have not declared (and thus created) $foo before), while writing

        Oh will it now?

        $ perl -le " use warnings; use strict; my $foo; $foo++; print $foo " 1

        So these two examples are not comparable. And I believe the second example actually is where we see autovivication at work.

        Yes, they really are exactly comparable, and no, the second example is not autovivification any more than the first example is autovivification -- neither is autovivification

        Growing an array is not autovivification. JavaScript doesn't support autovivification , try it if you have firefox ( Ctrl+Shift+K )

        [03:41:08.755] var noauto = [ 0, 1 ]; noauto[ 6 ] = 66; noauto ; [03:41:08.764] [0, 1, , , , , 66] [03:43:06.834] noauto[2].failToAutoVivify = 12; noauto; [03:43:06.843] TypeError: noauto[2] is undefined

        JavaScript supports growing arrays but not autovivification, but perl does supports it, undef becomes a hashref if you treat it like a hashref

        $ perl -MData::Dump -le " my $auto = [ 0, 1 ]; $$auto[6]=66; dd $auto; + " [0, 1, undef, undef, undef, undef, 66] $ perl -MData::Dump -wle " my $auto = [ 0, 1 ]; $$auto[6]=66; $$auto[2 +]{VIVIFY}=12; dd $auto; " [0, 1, { VIVIFY => 12 }, undef, undef, undef, 66]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-08-29 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls