Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: match with elements in array

by Marshall (Abbot)
on May 18, 2010 at 22:49 UTC ( #840577=note: print w/replies, xml ) Need Help??


in reply to match with elements in array

I like kennethk's idea. But since you asked about a hash way, I'll show a way...see below. This of course is a memory hog! But 14,000 hash keys wouldn't necessarily be considered "huge". The code to build the hash look up table is simple and then you would just query it for values of $vector. How efficient this thing has to be is of course a matter of your application. Something like this might be more appropriate if the "lane buckets" weren't contiguous or if there were multiple ranges associated with each lane.
#!/usr/bin/perl -w use strict; use Data::Dumper; my @lanes = ( [(200900..202543)], [(202544..204187)], [(204188..205831)], [(205832..207475)], [(207476..210119)], [(210120..211763)], [(211764..213407)], [(213408..215051)], ); my %hash; my $lane =1; foreach my $aref (@lanes) { @hash{@$aref} = ($lane) x @$aref; $lane++; } my $vector = 210121; print $hash{$vector}; #prints 6

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2019-11-18 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (90 votes). Check out past polls.

    Notices?