Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Autovivification in perl (less clicks of the keyboard to create arbitrarily complex data structures)

by Anonymous Monk
on Jan 10, 2014 at 13:47 UTC ( [id://1070120]=note: print w/replies, xml ) Need Help??


in reply to Autovivification in perl

This is the use, which would you rather have to type?
use Data::Dump qw/ dd /; { my @blah; $blah[0] = []; $blah[0][$i] = []; $blah[0][$i][$j] = []; $blah[0][$i][$j][$k] = {}; $blah[0][$i][$j][$k]{life} = 42; dd( \@blah ); } { my @blah; $blah[0][$i][$j][$k]{life} = 42; dd( \@blah ); } __END__

The same in javascript* where they don't have autovivification

$blah[0] = new Array(); $blah[0][$i] = new Array(); $blah[0][$i][$j] = new Array(); $blah[0][$i][$j][$k] = new Object(); $blah[0][$i][$j][$k].life = 42;

* instead of new Array() you can type [] (like in Perl ), and instead of new Object() you can type {} (like in perl)

when we use this autovivification in perl.

All the time :)

See Data::Diver and Re: Parsing SOAP::Lite results (with Data::Diver), and Lexical scoping like a fox, Variable Scoping in Perl: the basics, ... 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?, TLC: Programming: Perl: Autovivification : What is it and why do I care?, , perlreftut#Making References , references quick reference

Modern Perl

Reference Coercion

Using a dereferencing operation on a non-reference turns that value into a reference (Autovivification, pp. 62) is handy when manipulating nested data structures (Nested Data Structures, pp. 61):

  • Comment on Re: Autovivification in perl (less clicks of the keyboard to create arbitrarily complex data structures)
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-16 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found