Bindo has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I just want to learn how to work with hashes within subroutines. For an instance say that I want to get all the users whos ids are greater than 500 from /etc/passwd file and list thier home directories in the following manner
dave --> /x02/dave
jack --> /x02/jack
Following script would only print information pertaning to one user.my %UID_PATH; my %ARR1 = uid(); foreach my $TAB (keys %ARR1) { print "User --> $TAB, Value --> $ARR1{$TAB}\n"; } sub uid { my %UID_PATH; open (FH1, "</etc/passwd") || die "Can't Open : $!"; while (<FH1>) { my %UID_PATH; my @UID = split (/:/, $_); if ($UID[2] > 500) { my $USER = "$UID[0]"; %UID_PATH = ("$USER" => "$UID[5]"); #print "$USER --> $UID_PATH{$USER}\n"; return %UID_PATH; } } }
Please can someone help me out. I just want to to display all users with a UID greater than 500. Still main rational here is to understand hash behavior in subs. :)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Retunining hash values from subroutines
by bart (Canon) on Jun 20, 2013 at 10:51 UTC | |
Re: Retunining hash values from subroutines
by hbm (Hermit) on Jun 20, 2013 at 11:31 UTC | |
Re: Retunining hash values from subroutines
by packetstormer (Monk) on Jun 20, 2013 at 11:33 UTC | |
by Bindo (Acolyte) on Jun 20, 2013 at 12:16 UTC | |
by packetstormer (Monk) on Jun 20, 2013 at 12:59 UTC | |
by Laurent_R (Canon) on Jun 20, 2013 at 21:24 UTC | |
Re: Retunining hash values from subroutines
by BillKSmith (Monsignor) on Jun 20, 2013 at 10:52 UTC | |
Re: Retunining hash values from subroutines
by kcott (Archbishop) on Jun 20, 2013 at 21:21 UTC |
Back to
Seekers of Perl Wisdom