An additional remark is that both your $people hashref and my %people hash are lexically scoped to the subroutine and not accessible outside the sub. I guess you will probably need to return it to the calling routine or share it one way or another with the caller if it needs to be used outside the sub.sub getPeople { my $file = 'list.txt'; my %people; open( LIST, "< $file" ) or die "Can't open $file : $!"; while (my $row = <LIST> ) { my ($id, $first, $last, $age) = (split /[\s=]/, $row)[1, 3, +5, 7]; $people{$id} = { id => $id, first => $first, last => $last, age => $age }; } print Dumper(\$people); print "The person with ID 3 is $people{3}{first} $people{3}{last} +\n"; close LIST; }
As a final note, I think you should compare your indentation with mine: I think that mine shows more clearly that the code after the end of the while loop still belongs to the subroutine definition. Taking the habit or properly indenting your code will save you a lot of debugging time when things get a bit complicated.
In reply to Re^5: Having an Issue updating hash of hashes
by Laurent_R
in thread Having an Issue updating hash of hashes
by perlguyjoe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |