Let me first say that this site rocks! I'm a Perl newbie and have found the information here to be extremely useful in learning Perl.
Right, then! I've got a hash of x,y data (mass = keys,intensities = values). In my script, I search for a mass key that represents the largest intensity within a window of +- 0.3 mass units. I've populated my hash with mass data to the nearest 0.1 units. Is there anyway to extract the keys matching the tolerance without the for loop. I'm doing lots of these calls in my script, so any speed gained here would be helpful. Cheers!
#subroutine that gets the largest intensity of the mass ($_[0] #within a mass tolerance window of +- 0.3. sub getIntensity { my $mLo = $_[0] – 0.3; my $mHi = $_[0] + 0.3; my $intensity = 0; for ( my $i = $mLo; $i <= $mHi; $i += 0.1 ) { $i = sprintf("%.1f", $i); if ( exists $massint{ $i } ){ if ( $massint{ $i } > $intensity ){ $intensity = $massint{ $i } } } } return $intensity; }
In reply to speeding up a hash lookup by chinman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |