[OT] Help with Perl Script
jack craig
jcraig at extraview.com
Wed Apr 21 15:43:50 UTC 2010
google is perl's best friend, google, 'perl sort arrays' ...
good luck, jackc...
On 04/21/2010 01:21 AM, Dan Track wrote:
> Hi,
>
> I appreciate that this is off-topic and wholly understand if I don't
> get an answer, although one would really appreciate it if I do get an
> answer :)
>
> Basically to improve my perl scripting, I've given myself a challenge
> where I would like to order the users in the passwd file by uid, I
> could do this in another language but I'm focused on improving my perl
> scripting. The script I've written so far is below, I've managed to
> figure out hashes and have read in the file and created a hash of
> hashes. It's after this that I'm stuck, I'm struggling to order the
> hash of hashes by uid and then print the ordered list out? Can someone
> please help me to finish this off? I'm trying to avoid using perl
> modules as I want to improve my scripting ability.
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my ( $login, $p, $uid, $gid, $gecos, $dir, $s );
> my $k;
> my $v;
> my $key;
> my $ip;
> my $value;
> my @sorted=();
>
> my @uid=();
>
> my %HoH = ();
>
> my $file='/etc/passwd';
>
> open( PASSWD, "< $file" ) or die "Can't open $file : $!";
>
> while(<PASSWD> ) {
> ( $login, $p, $uid, $gid, $gecos, $dir, $s ) = split( ':' );
>
> $HoH{ $login }{ 'login' } = $login;
> $HoH{ $login }{ 'p' } = $p;
> $HoH{ $login }{ 'uid' } = $uid;
> $HoH{ $login }{ 'gid' } = $gid;
> $HoH{ $login }{ 'gecos' } = $gecos;
> $HoH{ $login }{ 'dir' } = $dir;
> $HoH{ $login }{ 's' } = $s;
> #print "$login\n";
> }
>
> close PASSWD;
>
> #while (($k,$v) = each %HoH) {
> # print "While Loop: $k\n";
> #}
>
> #foreach $ip (keys %HoH) {
> # print "First For: $ip\n";
> # while (($key, $value) = each %{ $HoH{$ip} } ) {
> # print "$key = $value \n";
> # }
> # print "\n";
> #}
>
> #while ( ($k, $v) = each %HoH ) {
> # print "$k: \n";
> # while ( ($key, $value) = each %$v ) {
> # print "$key=$value ";
> # }
> # print "\n";
> #}
>
> #for $k ( sort keys %HoH ) {
> # print "$k: ";
> # for $v ( sort keys %{ $HoH{$k} } ) {
> # print "$v=$HoH{$k}{$v} ";
> # }
> # print "\n";
> #}
>
> #@sorted = sort { $HoH{$a} cmp $HoH{$b} } keys %HoH;
> #print "@sorted\n";
>
> #foreach my $sorted ( sort { $HoH{$a}->{"uid"} cmp $HoH{$b}->{"uid"} }
> keys %HoH)
> foreach my $sorted ( sort { $HoH->{$a}{uid} cmp $HoH->{$b}{uid} } keys %HoH)
> {
> print STDOUT "$HoH{$sorted}\n"
> }
>
> Thank in advance for any help.
>
> Dan
>
--
Jack Craig
Software Engineer
831.461.7100 x120
www.extraview.com
More information about the users
mailing list