___________001 #!/usr/bin/perl -w 002 use strict; 003 my %USERS; 004 foreach my $file (<passwd.*>) { 005 open(PASSWD,$file); 006 my $strip=$file; 007 $strip =~ s/passwd\.//; 008 while(<PASSWD>) { 009 my($login,$gcos) = (split(':',$_))[0,4]; 010 011 if(exists $USERS{$login}) { 012 push(@{$USERS{$login}},$strip); 013 } else { 014 $USERS{$login} = [$gcos,$strip]; 015 } 016 } 017 close(PASSWD); 018 } 019 020 open(NEWFILE,">endstrx") || print "Can't open servup: $!\n"; 021 foreach my $login (sort keys %USERS) { 022 print NEWFILE "$login:".shift(@{$USERS{$login}}).":"; 023 print NEWFILE join(':',@{$USERS{$login}})."\n"; 024 } 025 close(NEWFILE); 001 - 002 = Get the script ready to run turning warnings and strict on 003 = Get the hash %USERS ready for later 004 = loop threw all files matching the glob 'passwd.*' 005 = open the file found and give it the handle PASSWD 006 - 007 = copy the file name and remove the part before the first . 008 = loop over the lines of the file 009 = split the line on :, take the 1st and 4th element and putt +ing them in $login, and $gcos respectivly 011 = check if an element already exists in the hash with that u +ser name 012 = if so then push this file name onto the list at that key +. 014 = otherwise start a list at this key with the gcos and the + filename 017 = close the file 020 = open a new file for output with teh name endstrx and the h +andle NEWFILE 021 = loop over the keys of the USER hash, sorting them alphabet +icaly 022 = print the users name, followed by the first element of the + list at that key, 023 = then join all the file names together and print them as we +ll 025 = close the file
janitored by ybiC: s/pre/code/ as per Monastery convention
In reply to Re: hash of hashes -newbie question?
by eric256
in thread hash of hashes -newbie question?
by tux242
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |