Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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!

Kaiti
Swiss Army Nerd

In reply to Iterating through a HoHoH ... does a particular sub-hash exist? by jedikaiti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (14)
    As of 2014-09-02 19:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (29 votes), past polls