If your keys are always numeric, the you shoudln't be using a hash. That's what arrays are for...:)

Notice thta this way, your don't need to sort in display().

use strict; my @array = ( 'angel', 'buffy', 'cordelia', 'dawn', 'ethan', 'faith', 'giles', ); reorder(5,'up'); # ethan moves up: swaps place with dawn display(); sub reorder { my ($item,$direction) = @_; ### Adjust index as arrays start at 0 $item--; # Down is here :) - you should add bounds checking! my $other = $direction eq 'up' ? $item - 1 : $item + 1; @array[$item, $other] = @array[$other, $item]; return; } sub display { print '-' x 30,"\n"; foreach my $key (0..$#array) { printf("%12s is now number %d\n",$array[$key],$key+1); ##Adjust key +for display. } print '-' x 30,"\n"; } __END__ D:\Perl\test>test ------------------------------ angel is now number 1 buffy is now number 2 cordelia is now number 3 ethan is now number 4 dawn is now number 5 faith is now number 6 giles is now number 7 ------------------------------

In reply to Re: Swapping Two Hash Values by BrowserUk
in thread Swapping Two Hash Values by Cody Pendant

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.