That's the nature of hashes - in order to give you constant-time access to any element in the hash, regardless of size, the tradeoff is that perl gets to control the ordering of keys. And that order should be somewhat random to provide this ability (look up what "hashing" is in computing science).

What you want is a list. Say an order of operations:

my @datums = ( { field => 'out', snmptag => '1.3.6.1.2.1.2.2.1.16.1', opt => 'U' }, { field => 'in', snmptag => '1.3.6.1.2.1.2.2.1.10.1', opt => 'D' }, # ... # read 'em in (probably don't care about order, but what the heck) for my $datum (@datums) { chomp($data{$datum->{field}} = qx/snmpget -v1 -c $string $ip $datum- +>{snmptag}/) if $opt{$datum->{opt}}; } $update_string = join ':', 'N', map { /: (\d+)/; $1 } @data{ map { $_- +>{field} } @datums };
(untested) I don't really understand what you're doing, so I could have easily misinterpreted some of your code. Note that we probably could get rid of the %data hash, too, and instead just populate a list directly in your order.
my @fields = map { qx/snmpget -v1 -c $string $ip $_->{snmptag}/ =~ /: (\d+)/; $1 } grep { $opt{$_->{opt}} } @datums; $update_string = join ':', 'N', @fields;
Probably a bit easier to read. (and just as tested)


In reply to Re: hash components out of order by Tanktalus
in thread hash components out of order by vortmax

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.