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

Re: A better way of lookup?

by KurtSchwind (Chaplain)
on Apr 28, 2015 at 11:38 UTC ( [id://1124994]=note: print w/replies, xml ) Need Help??


in reply to A better way of lookup?

So in both perl and javascript you can use the same algorithm if you'd like.

Start with an arrary with all of your breakpoint values. I'll abridge here.

var breakpoints = [ 25000 , 50000 , 150000 ]; my @breakpoints = ( 25000 , 50000 , 150000 );

Then push the new value in. For our example I'll push in 50123.

breakpoints.push(50123); push(@breakpoints, 50123);

Sort them. Do an index-of on your value - 1.

breakpoints.sort(); var return_value = breakpoints[breakpoints.indexOf(50123) - 1]; @breakpoints = sort @breakpoints; # Use your favourite perl method for indexOf like above
In javascript this is actually cleaner than perl. The sort in javascript doesn't have to do a copy at the end and it has the 'indexOf' operator built in. </code>
--
“For the Present is the point at which time touches eternity.” - CS Lewis

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-23 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found