I have a hash, much like the one below:
%cmdData = ( 'command_1' => { 'subfields' => [], 'description' => 'descriptive text', 'safety' => 'SAFE' }, 'command_2' => { 'subfields' => [ { '_size' => 32, 'state_conversions' => [], 'subfield_name' => 'sub_1', '_fsw_type' => 'UINT32', }, { '_size' => 32, 'state_conversions' => [], 'subfield_name' => 'sub_2', '_fsw_type' => 'UINT32', }, ], 'description' => 'descriptive text', 'safety' => 'SAFE', } )
What I want to do is to loop through each command (key) in the hash, and do X if it has one or more subfields, and Y if it has 0 subfields. So I have a foreach loop in my code (below), with an if/else statement.
Problem is, I can't get the if/else conditions right. I have tried it as below, and I have tried if (not defined $cmdData->{$cmd}{subfields}). In both cases, it prints all the command names, followed by "defined".
#!/usr/bin/perl -w use strict; use warnings; use Storable; my %info; my $cmdData = retrieve('file.pm') foreach my $cmd ( sort { $cmdData->{$a}{pkt_target} cmp $cmdData->{$b} +{pkt_target} || $a cmp $b } keys %{$cmdData}) { print "$cmd "; if ($cmdData->{$cmd}{subfields} eq "") { print "undef\n"; } else { print "defined\n"; } };
Yes, I can change the hash I'm working with - sort of. I have a preprocessing script that is compiling a whole mess of hashes of the same format into one big one in a file (so it's easier to iterate through later). I can change that script to output something nicer, but I still need to know how to tell if there are subfields or not (this is how they appear in the original hashes I'm pulling from) so I can fix them.
Thanks a million, Monks!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |