in reply to Re: Re: hashes - finding values
in thread hashes - finding values
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?
|
|---|