in reply to Re: hashes - finding values
in thread hashes - finding values

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 #

Replies are listed 'Best First'.
Re: Re: Re: hashes - finding values
by Zaxo (Archbishop) on Jul 03, 2003 at 14:29 UTC

    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

Re: Re: Re: hashes - finding values
by BrowserUk (Patriarch) on Jul 03, 2003 at 14:35 UTC

    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