[aklug] Re: perl lists question, sorting, sort of.

From: Leif Sawyer <lsawyer@gci.com>
Date: Tue Aug 17 2010 - 12:27:19 AKDT

instead of assigning it raw, push it onto a stack:

my ${HOSTS}

( your loop to read the hosts file)
     push @{$HOSTS{$IP}},$name

then just pop 'em off the stack when you're ready to print:

  for my $IP (sort keys(%HOSTS)) {
     print "$IP";
     foreach $host (@{$HOSTS{$IP}}) {
         print "\t$host";
        }
        print "\n";
   }

> -----Original Message-----
> From: aklug-bounce@aklug.org [mailto:aklug-bounce@aklug.org]
> On Behalf Of Lee
> Sent: Tuesday, August 17, 2010 12:05 PM
> To: aklug@aklug.org
> Subject: [aklug] perl lists question, sorting, sort of.
>
> I'm consolidating and cleaning up some /etc/hosts here at
> work, and using it as a perl learning experience (rather than
> just doing it by hand).
>
> A short example of what I've got is:
>
> 172.17.21.40 mlpowks1c
> 172.17.21.40 mlpowks1d
> 172.17.21.41 mlpowks2
> 172.17.21.42 mlpowks3
> 172.17.21.43 mlpowks4
> 172.17.21.40 mlpowks1a
> 172.17.21.44 mlpowks5
> 172.17.21.46 mlpswks1
> 172.17.21.47 mlpswks2
> 172.17.21.46 drafts
> 172.17.21.48 mlpswks3
> 172.17.21.40 sat-ops
>
> Notice there are 4 names for 172.17.21.40 and 2 names for
> 172.17.21.46. As you may guess, there can be several lines
> in a hosts file for any given ip.
>
> What I'd like to do is consolidate this so it looks something like:
>
> 172.17.21.40 mlpowks1c mlpowks1d mlpowks1a sat-ops
> 172.17.21.41 mlpowks2
> 172.17.21.42 mlpowks3
> 172.17.21.43 mlpowks4
> 172.17.21.44 mlpowks5
> 172.17.21.46 mlpswks1 drafts
> 172.17.21.47 mlpswks2
> 172.17.21.48 mlpswks3
>
> Apparently, from what I read, doing the hash thing will
> simply take the last element, since the 'key' (that is the
> ip) is exclusively indexed. Testing seems to bear this out.
>
> I can also brute force it was a lot of loops and
> conditionals. That's an option.
>
> But, before I do that, is there an easy, 'perl-y' way to do this?
>
> Thanks!
>
>
> ---------
> To unsubscribe, send email to <aklug-request@aklug.org> with
> 'unsubscribe' in the message body.
>
>
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue Aug 17 12:27:27 2010

This archive was generated by hypermail 2.1.8 : Tue Aug 17 2010 - 12:27:27 AKDT