Hello Monks,

I'm looping through a hash that has about 240 elements. The order of the hash defiantly matters, so I used "use Tie::IxHash;" to preserve the ordering of the hash.

So in the code below, I loop through the hash and have a counter that is set to 1 and resets when its > 8.
But for some reason it skips what should be the 9th element in the hash on each reset.

The code below contains only a small portion of the hash (the first 32 elements).

And when I execute the code it prints the following:
Gi3/1 Gi3/2 Gi3/3 Gi3/4 Gi3/5 Gi3/6 Gi3/7 Gi3/8
Gi3/10 Gi3/11 Gi3/12 Gi3/13 Gi3/14 Gi3/15 Gi3/16 Gi3/17
Gi3/19 Gi3/20 Gi3/21 Gi3/22 Gi3/23 Gi3/24 Gi3/25 Gi3/26
Gi3/28 Gi3/29 Gi3/30 Gi3/31 Gi3/32

As you can see it skips would would be the 9th element, going from Gi3/8 to Gi3/10 and so on...

I'm assuming it's somehting to do with my logic. Anyone know what I'm doing wrong?

use warnings; use strict; use Tie::IxHash; tie (my %ifSpeeds, "Tie::IxHash"); %ifSpeeds = ( "Gi3/1" => "1000000000", "Gi3/2" => "100000000", "Gi3/3" => "1000000000", "Gi3/4" => "1000000000", "Gi3/5" => "100000000", "Gi3/6" => "1000000000", "Gi3/7" => "1000000000", "Gi3/8" => "1000000000", "Gi3/9" => "100000000", "Gi3/10" => "1000000000", "Gi3/11" => "1000000000", "Gi3/12" => "1000000000", "Gi3/13" => "1000000000", "Gi3/14" => "100000000", "Gi3/15" => "1000000000", "Gi3/16" => "1000000000", "Gi3/17" => "1000000000", "Gi3/18" => "1000000000", "Gi3/19" => "100000000", "Gi3/20" => "1000000000", "Gi3/21" => "100000000", "Gi3/22" => "1000000000", "Gi3/23" => "1000000000", "Gi3/24" => "1000000000", "Gi3/25" => "100000000", "Gi3/26" => "100000000", "Gi3/27" => "100000000", "Gi3/28" => "100000000", "Gi3/29" => "100000000", "Gi3/30" => "100000000", "Gi3/31" => "1000000000", "Gi3/32" => "1000000000" ); my $counter = 1; foreach my $key ( keys %ifSpeeds ) { if ($counter <= 8) { print "$key "; $counter++; } else { print "\n"; $counter = 1; } }



Any thoughts would be much appreciated.

Thanks,
Matt


In reply to Looping Over Hash Skips an Element? by mmartin

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.