Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Maybe this does what you want:
#!perl use strict; use warnings; my (@array, %hash); sub push_item { # push_item(key => value) return 0 if defined $hash{$_[0]}; push @array, [$_[1], $_[0], scalar @array]; $hash{$_[0]} = $array[-1]; return 1; } sub swap_array { # swap_array(index1, index2) @array[$_[0], $_[1]] = @array[$_[1], $_[0]]; ($array[$_[0]]->[2], $array[$_[1]]->[2]) = ($array[$_[1]]->[2], $array[$_[0]]->[2]); } sub get_array_pos_by_key { # get_array_pos_by_key(key) return undef unless exists $hash{$_[0]}; $hash{$_[0]}->[2]; } sub get_hash_key_by_index { # get_hash_key_by_index(index) return undef unless $_[0] < @array; $array[$_[0]]->[1]; } # populate push_item('foo' => 'bar'); push_item('joe' => 'banana'); push_item('foe' => 'friend'); push_item('gin' => 'tonic'); push_item('ice' => 'age'); print get_array_pos_by_key('gin'), "\n"; print get_array_pos_by_key('joe'), "\n"; print $hash{'gin'}->[0], ' ', $hash{'joe'}->[0], "\n"; print $array[3]->[0], ' ', $array[1]->[0], "\n"; swap_array(get_array_pos_by_key('gin'), get_array_pos_by_key('joe')); print get_array_pos_by_key('gin'), "\n"; print get_array_pos_by_key('joe'), "\n"; print $hash{'gin'}->[0], ' ', $hash{'joe'}->[0], "\n"; print $array[3]->[0], ' ', $array[1]->[0], "\n";
I'm in a bit of a hurry now, but it maintains both a hash and an array of array references whith item, array index and hash key in it. The data structures don't have to be moved, which is an advantage for large data structures, but more than one data item has to be modified when items are swapped. I hope the code explains the rest.
Hope this helped.

Entropy is the tendency of everything going to hell.

In reply to Re: Re-orderable keyed access structure? by CombatSquirrel
in thread Re-orderable keyed access structure? by BrowserUk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (10)
    As of 2020-02-28 16:48 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (125 votes). Check out past polls.