Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Occurence List

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


in reply to Re^3: Occurence List
in thread Occurence List

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]


Comment on Re^4: Occurence List
Select or Download Code
Re^5: Occurence List
by Anique (Acolyte) on Oct 11, 2012 at 11:45 UTC

    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.

      Can you cite your sources? Where did you learn about autovivification? in perl?

      You offered these two snippets but they're not perl :)

      $ perl -le " my $bar[396]++; " syntax error at -e line 1, near "$bar[" Execution of -e aborted due to compilation errors. $ perl -le " my $bar[396]; " syntax error at -e line 1, near "$bar[" Execution of -e aborted due to compilation errors.

      $foo is a named variable, it has a name, it doesn't need to be autovified, it exists by virtue of being named, just like @bar is a named variable, it has a name, it doesn't need to be autovivified, it exists by virtue of being named. By default all variables are global.

      When you add my $foo; my @bar; you declare $foo and @bar to be lexical variables (not global)

      it also has the side effect of satisfying use strict 'vars'; by letting it know you didn't make a typo, that you intended to have variables named $foo and @bar

      neither is related to what we call autovivification ; autovivification is about using references to create complex data structures with less clicks of the keyboard

      autovivification does not refer to declaring (naming) variables, it is a feature that saves you typing, it saves you from having to write  $foo[6] ||= []; $foo[6][6]=6; you can simply write  $foo[6][6] = 6; and  $foo[6] autovivifies/ becomes an arrayref

      explicit, assigning an array ref to  $foo[6] ||= []; $foo[6][6]=6;

      implicit, autovivifying an array ref, treat it like an array ref, it becomes an array ref  $foo[6][6] = 6;

      autovivification, autovivification, The Bad, the Ugly, and the Good of autovivification, References quick reference, autovivification, autovivification, undefined value as an ARRAY reference sometimes ok, What does Autovivify mean?, Should perl auto vivify here?, Tutorials ...

        Thank you for further explaining autovivication to me. However, I would like to note that I said not to use the two snippets you quoted. I am not surprised they result in a syntax error :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-12-25 13:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls