As pointed out
above, your sample data and your description of the task will lead to the same numeric string being treated as a hash key for several distinct values.
Based on what you describe as the desired output, it looks like you want to keep just the first value assoicated with a given key, and ignore later values. (But you didn't actually say this, so I'm just guessing.) If that is the case, you would want to alter the code in one of the earlier replies so that you check for the existence of a hash key before you assign a value to it. Using thesa's solution, for instance:
my %hash = ();
while(<DATA>) {
chomp;
my ($k, $v) = split / \s+ \d+ , \s+ /x;
$hash{$k} = $v unless exists $hash{$k};
}
This will make sure that once a value is assigned to a given hash key, no other value will be assigned to that key (assuming this is what you want).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.