Re: [OT] Help with Perl Script

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

 



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

-- 
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]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux