Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: "print" of nonexistent element is actually altering a hash

by larrymenard (Novice)
on Feb 17, 2020 at 19:34 UTC ( [id://11113070]=note: print w/replies, xml ) Need Help??


in reply to "print" of nonexistent element is actually altering a hash

Thank you Monks, the "exists()" is exactly what I needed to test whether the key exists or not.

Replies are listed 'Best First'.
Re^2: "print" of nonexistent element is actually altering a hash
by bliako (Monsignor) on Feb 18, 2020 at 07:47 UTC

    right, but be careful because AnomalousMonk warned that exists() will autovivify in some cases. Perhaps you want to run your program with and without no autovivification; and diff your resultant data structure.

Re^2: "print" of nonexistent element is actually altering a hash
by Anonymous Monk on Feb 18, 2020 at 00:18 UTC
    Hi
    use Data::Diver qw/ Dive /; print Dive( \%hash, qw/ key0 key1 key2 /); ## doesn't alter %hash
Re^2: "print" of nonexistent element is actually altering a hash
by Anonymous Monk on Feb 18, 2020 at 02:07 UTC
    Yes, as other "Anonymous Monks" have said – a feature. Because, if you are legitimately trying to use something like $hash{'www'}{'xxx'}{'yyy'}{'zzz'}, it is "hugely convenient" that Perl will automagically create $hash{'www'} then $hash{'www'}{'xxx'} then $hash{'www'}{'xxx'}{'yyy'}, and maybe even $hash{'www'}{'xxx'}{'yyy'}{'zzz'}, all without asking. This very simple trick bypasses a lot of tedium and is "usually" beneficial. Just not in your case!

      Mike, you do realize you could save yourself the time and hassle of posting here and both the OP and PerlMonks would be just as well off. Don't you?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found