Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: define a hash-value, using hash-ref and a list of keys

by Loops (Curate)
on Oct 29, 2014 at 13:13 UTC ( [id://1105473]=note: print w/replies, xml ) Need Help??


in reply to define a hash-value, using hash-ref and a list of keys

You could make a subroutine, but you can use //= assignment for the equivalent of the code example you gave:
$cf{$k1}{$k2}{$k3}{"hlst"} //= "";
But that assumes the problem you're trying to solve is duplicating that variable twice.

Replies are listed 'Best First'.
Re^2: define a hash-value, using hash-ref and a list of keys
by LanX (Saint) on Oct 29, 2014 at 17:11 UTC
    > $cf{$k1}{$k2}{$k3}{"hlst"} //= "";

    IMHO the best solution in this case where autovivification is wanted.

    For completeness if the perl version is to old for defined-or, one could use a ref to the hash value.

    $ perl use Data::Dumper; my $hash; my $rval = \ $hash{a}{b}{c}{"hlst"}; $$rval = "" unless defined $$rval; print Dumper \%hash; __END__ $VAR1 = { 'a' => { 'b' => { 'c' => { 'hlst' => '' } } } };

    or you can define an defined-or-assign function to do so

    use Data::Dumper; my $hash; defor( $hash{a}{b}{c}{"hlst"} , ""); print Dumper \%hash; sub defor { $_[0] = $_[1] unless defined $_[0]; } __END__ $VAR1 = { 'a' => { 'b' => { 'c' => { 'hlst' => '' } } } };

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found