Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Occurence List

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


in reply to Re: Occurence List
in thread 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."

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

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


Comment on Re^2: Occurence List
Download Code
Re^3: Occurence List
by Anique (Acolyte) on Oct 11, 2012 at 10:09 UTC

    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]

        when you write

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

        you declare $foo by saying my $foo; and then do something with it($foo++). Of course $foo++ would then not cause autovivication, because $foo already exists.

        The thing with array indexes and hash keys, is that we do not have to declare them before we can use them. There is no need to say my $bar[396];, or even my $bar[396]++; before you can assign anything to it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-10-26 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls