in reply to Autovivification with hash of hashes
The sub routine suggested by Aristotle suffers from the same problem. However there is no mention in the orignal post as to how complex the data structure might be, but I in my own experience I have been biten by similar problems.Can't use string ("key bar's value") as a HASH ref while "strict refs"
Now if we assign a scalar value to bar and try again it still works.#!/usr/bin/perl use strict; use warnings; my %foo; if(exists($foo{qux})) { print "\$foo{qux} exists"; } if(ref $foo{bar} eq 'HASH' && exists($foo{bar}{baz})) { print "\$foo{bar}{baz} exists\n"; } if(exists($foo{bar})) { print "\$foo{bar} popped into existence\n"; }
And finally with exist && exist to produce our error.#!/usr/bin/perl use strict; use warnings; my %foo; $foo{bar} = "key bar's value"; if(exists($foo{qux})) { print "\$foo{qux} exists"; } if(ref $foo{bar} eq 'HASH' && exists($foo{bar}{baz})) { print "\$foo{bar}{baz} exists\n"; } if(exists($foo{bar})) { print "\$foo{bar} popped into existence\n"; }
#!/usr/bin/perl use strict; use warnings; my %foo; $foo{bar} = "key bar's value"; if(exists($foo{qux})) { print "\$foo{qux} exists"; } if(exists($foo{bar}) && exists($foo{bar}{baz})) { print "\$foo{bar}{baz} exists\n"; } if(exists($foo{bar})) { print "\$foo{bar} popped into existence\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Autovivification with hash of hashes
by pg (Canon) on Jan 26, 2003 at 05:25 UTC | |
by Aristotle (Chancellor) on Jan 26, 2003 at 05:34 UTC | |
by trs80 (Priest) on Jan 26, 2003 at 23:14 UTC |