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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|