Greetings, esteemed monks!

I create a hash with spaces for where I need information (just declare the keys with a slice). I then loop through some lines of data, parsing it out for the info I need. Generally, the data I need are near the beginning of the data, and I would like to escape from the loop as soon as I have the data I need, rather than after I've parsed all the lines. Here's what I have so far:

#get running processes, users, 1 min load average, total mem, free mem + (ultimately display free mem %) my @raw=`top -b -n 1`; my %stats; @stats{'users','load','tmem','fmem','runproc'} = (); my $its=0; foreach my $line (@raw){ if ($line =~ /up\s.+\s(\d+)\suser.+\s+load\saverage:\s+(\d+\.\d{2}), +/){ $stats{users}=$1; $stats{load}=$2; } elsif ($line =~ /(?:Tasks|processes):.+\s+(\d+) running/i){ $stats{runproc}=$1; } elsif ($line =~ /^Mem:\s+(\d+)k\s+(?:total|av),.+used,\s+(\d+)k\s+fr +ee/){ $stats{tmem}=$1; $stats{fmem}=$2; } $its++; my $uds=0; foreach (values(%stats)){ defined or ++$uds; } if ($uds){ print "Still $uds undefined values"; } else{ print "I'm done!"; last; } } ## end foreach my $line (@raw) print "I looped $its times to collect the data I need from $host";
It just seems kind of clunky to me. That's partially because of the debugging output (it will eventually be silent--something like ($uds) || last; ). However, I'll probably want to do an additional check after the loop to make sure I didn't parse every single process and still come up empty on some of my stats due to (unexpected output from top|poorly formed REs).

Yes, I do know that "free" memory reported by top is not necessarily indicative of how much memory is available for new processes, due to buffering.


I like computer programming because it's like Legos for the mind.

In reply to How best to tell when my hash is "full" (all values defined)? by OfficeLinebacker

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.