I have a hash that's keyed with integers. The integers can be any ol thing, say for example:
hash{-10} = bar
hash{-1} = foo
hash{12} = baz
Now I want to change the keys of the hash to be sequential, starting at 0, so that I would have:
hash{0} = bar
hash{1} = foo
hash{2} = baz
I wrote this:
$counter=0;
foreach (sort keys(%hashone))
{
$hashone{$counter} = $hashone{$_};
delete $hashone{$_};
$counter++;
}
but it does odd things, like tossing out memory addresses and stuff. (incidentally, it always messes $hash{0} = some memory address) I also realize that I will have problems if my original hash was something like:
hash{-10} = bar
hash{1} = foo
hash{20} = baz
Can this be done without swapping values with a temporary hash?
Thanks!
Edited by davido: Added code tags.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.