Hello tangent.gardner, and welcome to the Monastery!

choroba++ has identified the logic error in your script. I just want to point out a few aspects of your code which could be improved.

  1. Perl is not C, and Perl’s function prototypes work quite differently. See perlsub#Prototypes. As a rule, it’s best to dispense with them altogether, unless you (a) have a particular use case which requires prototypes and (b) know how to use them correctly. See Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen.

  2. This snippet of code:

    while (!eof(IN) ) { $_ = <IN>; $DEBUG = 1 if ($_ =~ m/^DEBUG/)

    can be written more idiomatically like this:

    while (<IN>) { $DEBUG = 1 if /^DEBUG/;
  3. The test if ($_ > 0) { which immediately follows the previous snippet throws warnings whenever $_ is not a number. You should add an additional test; for example:

    use Scalar::Util qw( looks_like_number ); ... if (looks_like_number($_) && ($_ > 0)) {
  4. In a conditional test, the type of the operands should match the type of the operator. The numeric comparison operators are == != < <= > >= <=>, and their string counterparts are eq ne lt le gt ge cmp (see perlop#Equality Operators). So, e.g., this line:

    if ($timings{$item}{STATUS} != '2') {

    would be better written either as:

    if ($timings{$item}{STATUS} ne '2') {

    (if the hash value is stored as a string), or as:

    if ($timings{$item}{STATUS} != 2) {

    (if the hash value is stored as a number). Yes, Perl will generally perform a silent string-to-number or number-to-string conversion where required, but it’s better practice to make your code self-documenting by using operands and operators of matching types.

  5. die should be used only for exceptions, not for normal program termination. The final lines of your main code would be better written as follows:

    for (;;) { ... if ($COMPLETE) { show_status(); print "finished\n"; last; } } close $log or die "Cannot close file '$log_file': $!";

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: looping through hash of hashes by Athanasius
in thread looping through hash of hashes by tangent.gardner

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.