in reply to extract uniques and sort

If you're worried about unique *strings*, you can just try placing each item into a %hash key. When you're all finished, a keys %hash will net you all non-duplicated items. This act of finding unique elements from a list (which this can all be reduced to) is well documented on the site and in various Perl books.

If you're worried about catching duplicate *hostnames* (e.g. "host" v. "host.example.com" could be the same hostname if your domain is example.com), you'll have to try resolving each name individually, and perhaps storing the resulting IP address in the hash instead:

use Socket; while (my $item = <FILE>) { if (my $ip = gethostbyname($item)) { $hash{$ip} = gethostbyaddr($ip, AF_INET) || $item; } else { # no such hostname } } print "Unique items:\n"; print "$hash{$_}\n" for keys %hash;
Note that $ip is in packed form here.

Replies are listed 'Best First'.
RE: Re: extract uniques and sort
by geektron (Curate) on Sep 21, 2000 at 04:55 UTC
    if the hosts are all internal ( e.g. within a company intranet ), you can use Fastolfe's trick if you add the domain name to the hostname first

     $host .= '.foo.com' unless $host =~ /.foo.com$/;

    admittedly lazy, but i do use the trick here at work all the time. then, the problem of host vs host.foo.com (which are the same) is resolved.

    the IP resolution trick might not work for hosts w/ more than one IP address ( routers, for one )

      That isn't really necessary. So long as your domain is properly configured in your network subsystem (e.g. in /etc/resolv.conf), attempting to resolve "www" is equivalent to resolving "www.example.com".

      If you want to skip the step of actually attempting to resolve the hostnames, you can try to parse /etc/resolv.conf yourself and tack on each of the 'search' or 'domain' items (not sure what all is used), since you could technically have more than one. But if it's a custom app just for 1 location/purpose, I guess you could hard-code it without worry.

        it's actually easier to do it that way. my intranet is globally distributed, and /etc/resolv.conf isn't going to contain every host/ type of host.

        but for others, it's probably a valid approach.

RE: (2) extract uniques and sort (thanks, getting closer))
by ybiC (Prior) on Sep 21, 2000 at 19:33 UTC
    Thanks Fastolfe, for an informative reply, also to japhy, geektron, and I think tilly in the Chattbox.

    Actually, I'm looking for unique strings that happen to be hostnames.   The list is built using a variation of (code)) Cisco Pass Mass - IOS (deprecated by node 123464) to query a core switch for CDP neighbors (wiring closet "top o' stack" switches).   Next I feed the output back into the same script to find remaining switches in each wiring closet stack.   Then, I'll feed the complete list of core+closet switches back into the original (code)) Cisco Pass Mass - IOS (deprecated by node 123464) script to implement my config updates.

    I've now got my hands on the Perl Cookbook, pouring over example 4.6.   Looks like I'm getting closer to a solution.   Keys of Hash, eh?
        cheers,
        ybiC