in reply to hashes - finding values

Can you show us your data, what you expect, and what you get? I don't understand what is going wrong for you. The code you show looks ok and should do what you seem to want. I wonder if you have some references or something in your data which have a stringification you don't expect.

Your code can be simplified with slice initialization and grep:

my (%hash, @new_array); @hash{@numbers} = @values; @new_array = grep {exists $hash{$_}} @array; print @new_array;
That kind of construction treats the arrays and hashes as distinct objects rather than piles of data.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: hashes - finding values
by Anonymous Monk on Jul 03, 2003 at 13:59 UTC
    My data looks like this:
    # @numbers are shortened versions of @values to make numerical compari +son easier. @keys = ('-0.9940000000', '0.1190000000', '-0.0355000000'); @values = ('-0.993999999999999999', '-0.118999999999999999', '-0.03549 +9999999999999'); @array = ('0.1190000000', -0.0355000000'); # cmopare array to the keys in the array - extract the corresponding v +alues. # desired output --- # 0.118999999999999999 -0.035499999999999999 #

      That's the problem. The shortened stringy @numbers don't provide the same keys as the long ones in @array. Hash keys are strings and '0.1190000000' ne '.119'

      After Compline,
      Zaxo

      I don't think you need to loop or grep.

      Assuming that the values you are looking up are always in the hash, then you can use the array directly in a hash slice to produce the new array of the values like this

      my %hash; @hash{ qw[-0.9940000000 0.1190000000 -0.0355000000] } = qw[ -0.993999999999999999 -0.118999999999999999 -0.035499999999999 +999 ]; print Dumper \%hash; $VAR1 = { '0.1190000000' => '-0.118999999999999999', '-0.9940000000' => '-0.993999999999999999', '-0.0355000000' => '-0.035499999999999999' }; my @array = qw[ 0.1190000000 -0.0355000000]; my @new = @hash{ @array }; print @new; -0.118999999999999999 -0.035499999999999999

      The caveat is that if any of the values in @array don't exist in the hash as keys, then you will get undef in the corresponding elements of @new.

      However, this may actually be useful as it will give you a way of detecting that you had values in @array that didn't exist as keys in %hash.

      All of that said, if your ultimate goal is to compare floating point numbers with a fugde factor for floating point representation inaccuracies, then there are probably better, numerical ways of doing this, but you would need to show us how you are using this to be sure. I'm a bit thrown by your hash associating +0.1190000000 with -0.118999999999999999 ?? Or is that a typo?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller