Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Here's a solution. Add methods to your liking.
use strict; use warnings; package BrowserUKStruct; sub IDX_HASH () { 0 } sub IDX_ARRAY () { 1 } sub NODE_IDX_VAL () { 0 } # sort() assumes this is 0. sub NODE_IDX_KEY () { 1 } sub new { my ($class) = @_; return bless([ {}, # IDX_HASH [], # IDX_ARRAY ], $class); } sub append { my $self = shift(@_); while (scalar(@_)) { my $key = shift(@_); my $val = shift(@_); my $node = [ $val, # NODE_IDX_VAL $key, # NODE_IDX_KEY ]; push(@{$self->[IDX_ARRAY]}, $node); $self->[IDX_HASH]->{$key} = $node; } } sub get_val_by_key { my ($self, $key) = @_; return $self->[IDX_HASH]->{$key}->[NODE_IDX_VAL]; } sub get_val_by_pos { my ($self, $pos) = @_; return $self->[IDX_ARRAY]->[$pos]->[NODE_IDX_VAL]; } sub get_vals { my ($self) = @_; return map { $_->[NODE_IDX_VAL] } @{$self->[IDX_ARRAY]}; } sub get_key_by_pos { my ($self, $pos) = @_; return $self->[IDX_ARRAY]->[$pos]->[NODE_IDX_KEY]; } sub get_keys { my ($self) = @_; return map { $_->[NODE_IDX_KEY] } @{$self->[IDX_ARRAY]}; } sub sort { my ($self, $cond) = @_; # $cond, if provided, must use $_[0] and $_[1] instead of $a and $b +. $cond ||= sub { $_[0] cmp $_[1] }; @{$self->[IDX_ARRAY]} = sort { &{$cond}($a->[0], $b->[0]) } @{$self +->[IDX_ARRAY]}; return $self; } sub swap { my ($self, $pos1, $pos2) = @_; my $array = $self->[IDX_ARRAY]; my $item1 = $array->[$pos1]; my $item2 = $array->[$pos2]; $array->[$pos1] = $item2; $array->[$pos2] = $item1; } package main; { my $s = BrowserUKStruct->new(); $s->append(chien => 'dog'); $s->append(chat => 'cat'); $s->append(phoque => 'seal'); $s->append(poulet => 'chicken'); printf("\@orig_vals = %s\n", join(', ', $s->get_vals())); print("\n"); $s->swap(2, 3); printf("\@t1_vals = %s\n", join(', ', $s->get_vals())); printf("\$t1_val_at2 = %s\n", $s->get_val_by_pos(2)); printf("\$t1_val_at3 = %s\n", $s->get_val_by_pos(3)); printf("\$t1_val_phoque = %s\n", $s->get_val_by_key('phoque')); printf("\$t1_val_poulet = %s\n", $s->get_val_by_key('poulet')); print("\n"); $s->sort(sub { $_[1] cmp $_[0] }); printf("\@t2_vals = %s\n", join(', ', $s->get_vals())); print("\n"); printf("\@t3_key_at0 = %s\n", join(', ', $s->get_key_by_pos(0))) +; printf("\@t3_key_at1 = %s\n", join(', ', $s->get_key_by_pos(1))) +; } __END__ output ====== @orig_vals = dog, cat, seal, chicken @t1_vals = dog, cat, chicken, seal $t1_val_at2 = chicken $t1_val_at3 = seal $t1_val_phoque = seal $t1_val_poulet = chicken @t2_vals = seal, dog, chicken, cat @t3_key_at0 = phoque @t3_key_at1 = chien

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

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, 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (3)
    As of 2020-02-19 00:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (80 votes). Check out past polls.

      Notices?