in reply to restricting values to a nested datastructure
The word you're looking for is "autovivification".
You can avoid autovivifying hash and array slots by checking they exist first. Example:
#!/usr/bin/perl my %a; if (exists $a{'abd'} and exists $a{'abd'}->[0] and defined $a{'abd'}->[0]) { print "yes \n"; } use Data::Dumper; print Dumper \%a;
An alternative way of doing it would be to use the autovivification module (requires Perl >= 5.8.3). This produces nicer looking code, at the cost of a non-core dependency.
#!/usr/bin/perl no autovivification; my %a; if (defined $a{'abd'}->[0]) { print "yes \n"; } use Data::Dumper; print Dumper \%a;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: restricting values to a nested datastructure
by Marshall (Canon) on Dec 14, 2011 at 12:28 UTC | |
by tobyink (Canon) on Dec 14, 2011 at 13:17 UTC | |
|
Re^2: restricting values to a nested datastructure
by zwon (Abbot) on Dec 14, 2011 at 14:27 UTC | |
by Marshall (Canon) on Dec 14, 2011 at 15:28 UTC | |
by zwon (Abbot) on Dec 15, 2011 at 02:21 UTC | |
|
Re^2: restricting values to a nested datastructure
by Anonymous Monk on Dec 14, 2011 at 12:47 UTC |