Given that
$cntr is just a counter that is incremented for each data element of your list, you should probably use an array of hashes, rather than a reference to an hash of hashes. For example you could change your main procedure as follows (untested):
sub getPeople {
my ( $id, $first, $last, $age );
my $file = 'list.txt';
my @people; # using directly an array
# my $cntr; -- now useless
open( LIST, "< $file" ) or die "Can't open $file : $!";
while (my $row = <LIST> ) {
# $cntr++; -- now no longer useful
my ($id, $first, $last, $age ) = split( /\s/, $row );
$id = (split( /=/, $id ))[1];
$first = (split( /=/, $first ))[1];
$last = (split( /=/, $last ))[1];
$age = (split( /=/, $age ))[1];
push @people, {
'id' => "$id",
'first' => "$first",
'last' => "$last",
'age' => "$age"
};
}
}
I think that getting the individual values to be stored in the hashes could be made significantly simpler, but that's not what you asked for, I don't want to get off-topic at this point. If you're interested, other monks and myself can of course help you on that.
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.