in reply to Re^3: Having an Issue updating hash of hashes
in thread Having an Issue updating hash of hashes

I completely agree with your stance on readability. I manage some code written by my predecessor. While he was very creative in writing short compact and efficient code, it can be very hard to follow along, if your just looking in. Here is the final result:
#!/usr/bin/perl use Data::Dumper; use strict; use warnings; getPeople(); 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; }

Replies are listed 'Best First'.
Re^5: Having an Issue updating hash of hashes
by Laurent_R (Canon) on Jul 06, 2014 at 20:44 UTC
    This seems to be fine, but I do not understand why you insist on using a reference to a hash of hashes. Why not simply a hash of hashes? Something like this:
    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; }
    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.

    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.