For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > HoH efficiency









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author HoH efficiency
Rodrick Brown

2007-07-18, 3:59 am

Is there a more efficient way to handle the method I choose to unroll my HoH.
Thanks.

#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;

my @data = <STDIN>;

my ($user,$gid,$homedir,%userMap);
foreach(@data) {
/^#/ and next;
/(\w+):/ and $user = $1;
/\w+:*:(\d+):/ and $gid = $1;
/.*:(.*)$/ and $homedir = $1;
$userMap{$homedir}->{$user} = $gid;
}

#print Dumper(\%userMap);

while(my ($k0,$h0) = each(%userMap)) {
foreach my $shell ($k0) {
print $shell,"\n";
print "--------------\n";
while(my ($k1,$v0) = each(%$h0)) {
print "$k1 $v0\n";
}
print "\n";
}
#print $k,"\n\t",$_,"\n" foreach(values %$v);
}


___DATA___FILE___
##
# User Database
#
# Note that this file is consulted when the system is running in single-user
# mode. At other times this information is handled by one or more of:
# lookupd DirectoryServices
# By default, lookupd gets information from NetInfo, so this file will
# not be consulted unless you have changed lookupd's configuration.
# This file is used while in single user mode.
#
# To use this file for normal authentication, you may enable it with
# /Applications/Utilities/Directory Access.
##
nobody:*:-2:-2:Unprivileged User:/:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
postfix:*:27:27:Postfix User:/var/spool/postfix:/usr/bin/false
www:*:70:70:World Wide Web Server:/Library/WebServer:/usr/bin/false
eppc:*:71:71:Apple Events User:/var/empty:/usr/bin/false
mysql:*:74:74:MySQL Server:/var/empty:/usr/bin/false
sshd:*:75:75:sshd Privilege separation:/var/empty:/usr/bin/false
qtss:*:76:76:QuickTime Streaming Server:/var/empty:/usr/bin/false
cyrusimap:*:77:6:Cyrus IMAP User:/var/imap:/usr/bin/false
mailman:*:78:78:Mailman user:/var/empty:/usr/bin/false
appserver:*:79:79:Application Server:/var/empty:/usr/bin/false
clamav:*:82:82:Clamav User:/var/virusmails:/bin/tcsh
amavisd:*:83:83:Amavisd User:/var/virusmails:/bin/tcsh
jabber:*:84:84:Jabber User:/var/empty:/usr/bin/false
xgridcontroller:*:85:85:Xgrid Controller:/var/xgrid/controller:/usr/bin/false
xgridagent:*:86:86:Xgrid Agent:/var/xgrid/agent:/usr/bin/false
appowner:*:87:87:Application Owner:/var/empty:/usr/bin/false
windowserver:*:88:88:WindowServer:/var/empty:/usr/bin/false
tokend:*:91:91:Token Daemon:/var/empty:/usr/bin/false
securityagent:*:92:92:SecurityAgent:/var/empty:/usr/bin/false
unknown:*:99:99:Unknown User:/var/empty:/usr/bin/false
Jeff Pang

2007-07-18, 3:59 am


--- Rodrick Brown <rodrick.brown@gmail.com> wrote:

> Is there a more efficient way to handle the method I
> choose to unroll my HoH.
> Thanks.
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
> use Data::Dumper;
>
> my @data = <STDIN>;
>
> my ($user,$gid,$homedir,%userMap);
> foreach(@data) {
> /^#/ and next;
> /(\w+):/ and $user = $1;
> /\w+:*:(\d+):/ and $gid = $1;
> /.*:(.*)$/ and $homedir = $1;
> $userMap{$homedir}->{$user} = $gid;
> }
>
> #print Dumper(\%userMap);
>
> while(my ($k0,$h0) = each(%userMap)) {
> foreach my $shell ($k0) {
> print $shell,"\n";
> print "--------------\n";
> while(my ($k1,$v0) = each(%$h0)) {
> print "$k1 $v0\n";
> }
> print "\n";
> }
> #print $k,"\n\t",$_,"\n" foreach(values %$v);
> }
>


Hello,

Just give my version.It does the same work and seems
more clear.

use strict;
use warnings;

my %hash;
open FD,'/etc/passwd' or die $!;
while(<FD> ) {
chomp;
my ($user,$uid,$shell) = (split/:/)[0,2,6];
$shell ||= 'null';
push @{$hash{$shell}},[$user,$uid];
}
close FD;

for my $s (keys %hash) {
print "\n$s\n--------\n";
for my $p (@{$hash{$s}}) {
print "@$p\n";
}
}

__END__

HTH.



________________________________________
________________________________________
____
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
http://sims.yahoo.com/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com