In case you don't have line numbers turned on.
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
___________
Eric Hodges

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.