Hello monks!
I have a file with the following format:
ID FIRST_NUMBER SECOND_NUMBER
1 1 1
2 1 2
3 2 3
4 2 4
5 3 5
6 3 6
7 4 7
8 4 6
9 5 8
10 5 9
11 6 10
12 6 9
and I want to store this data in a data structure that I can easily browse. I cannot go with hashes, because neither ID nor NUMBER are unique, as you see.
By reading various tutorials, I thought that Hash of Hashes would be the thing for me, but can't seem to understand how to use it efficiently.
What I need to do is be able to read the data based on the SECOND_NUMBER. My guess is that ID, which is unique, could be of assistance, but I can't figure out how.
What I have tried is:
if($_=~/^(\d+)\t(.*)/)
{
$id=$1;
$rest=$2;
@split_rest = split(/\t/, $rest);
$first_num=$split_rest[0];
$second_num=$split_rest[1];
$HoH{$id}{$first_num} = $second_num;
The HoH works OK, but how can I search it when the only thing that I am given will be the FIRST_NUMBERS and NOT the IDs? For instance, if you take line 12(last line) my target is to be able to retrieve 9 (SECOND_NUMBER) when I am given 6 (FIRST_NUMBER) but not mistake 9 for 10 (which is in the above line).
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.