in reply to Hash table manipulation

First off, 0.423.0.523 is not a floating point number so you may need to specify your problem rather better than you have so far. Treating such strings as floating point numbers almost certainly will not do what you want.

Second, if you really are dealing with floating point numbers then whatever approach you use must take into account the fact that the number you use to generate a key (which is a string) most likely isn't the number you will get back if you treat the string as a number.

I strongly recommend that you tell us more about what you are trying to do because given the information we have from you so far I suspect that you still have plenty of trouble ahead of you.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Hash table manipulation
by sarvan (Sexton) on Jul 12, 2011 at 07:32 UTC
    Hello everyone, I will clearly tell you the problem.

    In my program what i will do is, i will take google's top 10 results for a query and i will take the snippet's or title's from each result and compute the similarity between this returned snippet and the original query..

    The computed similarity value is what i stored in the hash as key and the url's will be straightaway stored as hash value..

    for e.g $url={will contain the 10 url's grepped from xml result file} $value=sim();#will contain the similarity computed for all the ten res +ults; %hash; sim{ #here i compute similarity between query title and resulted snippet.. return $val;#i will return similarity value for each result } $hash{$value}=$url;#storing key & value in hash #i will sort the keys in hash in descending order to get the highest v +alue in top and print the url associated with that as a ouput..
    Now i want to filter it by some threshold like keys higher then 0.4 or something..

      Ok, given what you are doing your choice of using a hash is fair enough. The precision of the floating point values is fairly unimportant so any rounding or truncation that happens when using the numbers as keys is very unlikely to matter. To select the top N URLs I'd do something like:

      use strict; use warnings; my %urls = ( 0.999 => 'www.perlmonks.org', 0.65 => 'www.snakewranglers.org', 0.451 => 'www.jewelmerchants.org', 0.222 => 'www.coffeemerchants.org', 0.12 => 'www.scriptkiddies.org', ); my @inOrder = sort {$b <=> $a} keys %urls; my @topThree = splice @inOrder, 0, 3; print "$_: $urls{$_}\n" for @topThree;

      Prints:

      0.999: www.perlmonks.org 0.65: www.snakewranglers.org 0.451: www.jewelmerchants.org
      True laziness is hard work
        Hi GrandFather,

        Thanks for the post. But this sorting and getting the highest value, i have already done.

        I have asked for how could i get the values that are higher than certain threshold.. Plz let me know if my explanation is not adequate..