Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

I Ordered My Keys With Existing Values!

by abitkin (Monk)
on Jul 28, 2003 at 14:54 UTC ( #278435=perlquestion: print w/replies, xml ) Need Help??

abitkin has asked for the wisdom of the Perl Monks concerning the following question:

Okay, so here's the deal
I need a data structure that will store 2-7 data points (these points will be numbers from 0-50 with none repeated) in such a way as to preserve the order of the data points, and to make look up on a specific point easy.

I'm going to address the second part first, to make this easy for me, I'm tempted to put these items into a hash and use exists. eg.
sub find_point(){ my $ref = shift; my %hash = %$ref; my $key = shift; if(exists($hash{$key})){ ... } }

While this would be an ideal solution for finding a key quickly, trying to order the data based on the values associated with those keys seems to be quite a bit of extra effort. Eg.
sub points_list(){ my $ref = shift; my %hash = %$ref; my @order; foreach $item (keys %hash){ @order[int($hash{$item})]=$item; } return \@order; }

Now this wouldn't be so bad, but it may be done on a number of hashes like this. Does anyone see a better way?
==
Kwyjibo. A big, dumb, balding North American ape. With no chin.

Replies are listed 'Best First'.
Re: I Ordered My Keys With Existing Values!
by hardburn (Abbot) on Jul 28, 2003 at 15:03 UTC
    my @order = sort { $hash{$a} <=> $hash{$b} } keys %hash;

    Or you can try Tie-InsertOrderHash-0.01.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I thought that was just Tie::IxHash which is more common.

        *shrug* I did a search on CPAN for "order hash tie", and the InsertOrder module came up first. I've never found a use for keeping the insertion order on hashes, so I don't really have an opinion on either one.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

      This is an interesting piece of code, and while it is doing exactly what I'm doing, it doesn't address the underlying problem that I was trying to represent.

      Having to do this every time I want to display this hash will require quite a bit of extra calculations. I could store this array but then I have data duplication (memory/speed tradeoff) that I was hoping to avoid.
      ==
      Kwyjibo. A big, dumb, balding North American ape. With no chin.

        With 2 to 7 data points the extra calculations are something less than "quite a bit", unless you're displaying the values many, many times. I suspect the threshold would be quite high before you noticed a slowdown.
Re: I Ordered My Keys With Existing Values!
by traveler (Parson) on Jul 28, 2003 at 15:30 UTC
    Two more options: 1) use both an array and a hash. While I generally eschew such parallel data structures, this might be a good use; 2) add a hash key specifying the order then use that in a loop to print out the values.

    I dunno which is better for you. It likely depens on how often you access the "sorted" values and how many hashes you end up having.

    HTH, --traveler

Re: I Ordered My Keys With Existing Values!
by TomDLux (Vicar) on Jul 28, 2003 at 17:53 UTC

    In find_point() you pass in a reference to a hash, and then copy the hash from %$ref to %hash. If you're concerned about speed, dedreference the reference:

    if( exists $ref->{$key} ) {

    Since the hashes are tiny, 2 to 7 members, copy it from the beginning, rather than passing in a referernce.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://278435]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2020-05-28 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (165 votes). Check out past polls.

    Notices?