Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl- Reading sorted array elements into a hash with sorted keys

Tags:

arrays

hash

perl

so i have an array (say @array) with sorted values between 0 and 1, and also and a hash (say %hash) whose keys are sorted and are numbers between 0 and 1. the values for each key in the hash is 0. Now, I need to look at each element of @array, find the key in %hash which is immediately smaller than it, and increment the corresponding value by 1 . that is, the keys serve as a lowerbounds for intervals

if say

$array = (0.15,0.33,0.67,0.87) 
and %hash = ("0.25", 0, "0.50", 0, "0.75", 0)

and I take $array[1] = 0.33

then, I need to be able to determine that $array[1] is greater than 0.25 BUT less than 0.5 and, thus, increment the value for "0.25" by 1 giving me an updated hash %hash = ("0.25", 1, "0.50", 0, "0.75", 0).

I hope this made sense! thanks in advance!!!

like image 627
confused_766 Avatar asked Mar 22 '26 21:03

confused_766


1 Answers

Hash does not store keys in sorted order. You must rethink your approach to the problem.

like image 88
zvrba Avatar answered Mar 25 '26 10:03

zvrba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!