[OT] Help with Perl Script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux