Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: extract only smallest numbers

by grep (Monsignor)
on Jul 21, 2007 at 21:38 UTC ( #628034=note: print w/ replies, xml ) Need Help??


in reply to Re^2: extract only smallest numbers
in thread extract only smallest numbers

OK.

Couple of things first.

  • Use strict and warnings
  • Check to see if your open succeeds
  • Use split not a regex when you want to split
  • Indent your code - It makes reading the code easier on you and us
So your code should look something like (untested):
use strict; use warnings; open FILE, $my_file or die "Can't open $my_file\n"; my %hash; while (<FILE>) { my ($name, $code) = split(/\t/); }
Now you have some of the problems fixed. Now you need to test what's in the hash and $code.

One thing to remember is, that if you have not assigned a value to the hash for that key you'll get undef returned, which evaluates to 0 numerically. Since you are looking for the lowest number you'll want avoid the undef. So check the value from your hash with defined first. If you see that it is not defined, then you know to assign the value of $code whatever it is.

# Still untested if ( !defined( $hash{$name} ) ) { $hash{$name} = $code; }
If you have a value defined then check to see whats larger
# Still untested if ( !defined( $hash{$name} ) or $code < $hash{$name} ) { $hash{$name} = $code; }

Comment on Re^3: extract only smallest numbers
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://628034]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-09-15 04:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls