Professional Web Applications Themes

search for duplicates and sort array - PERL Miscellaneous

Rick L wrote: > > I know there is already plenty of info about sorting arrays and searching > for duplicates in arrays out there in the faq's. My questions varies > slightly and I haven't been able to figure out a solution yet. I'll give a > general idea of what my array looks like. The first column is IPs, the > second column is FQDNs, and the third column is classifications. So it > looks something like this: > > 12.12.12.12 blah.com A > 23.23.23.23 blah2.com A > 34.34.34.34 blah3.com B > 33.33.33.33 blah4.com B > 44.44.44.44 check.com ...

  1. #1

    Default Re: search for duplicates and sort array



    Rick L wrote:
    >
    > I know there is already plenty of info about sorting arrays and searching
    > for duplicates in arrays out there in the faq's. My questions varies
    > slightly and I haven't been able to figure out a solution yet. I'll give a
    > general idea of what my array looks like. The first column is IPs, the
    > second column is FQDNs, and the third column is classifications. So it
    > looks something like this:
    >
    > 12.12.12.12 blah.com A
    > 23.23.23.23 blah2.com A
    > 34.34.34.34 blah3.com B
    > 33.33.33.33 blah4.com B
    > 44.44.44.44 check.com C
    > 43.43.43.43 check2.com C
    >
    > What I've done is write up a script for users to be able to add/remove
    > entries from this array. There are two more things that I need to do in
    > order for this to be complete. First, if a user adds an entry, I need to
    > check the 1st or 2nd column to make sure that either the IP or FQDN don't
    > match anything else already listed in the array.
    Whenever someone wants to know if a <something> is a member of a set,
    then it springs to mind that a hash is desired, not an array.
    >I've tried to run this in
    > a while loop and if it comes across an entry that exists, exit and print an
    > error message, but that isn't working for me. Secondly, once an addition to
    > the list is made, I would like to sort the array. Here is the tricky part.
    > I want to keep each 3rd column grouping together, and within those groupings
    > I want to sort alphabetically based on the 2nd column. I realize that doing
    > this via a hash will probably make more sense, but I'm not very good at
    > creating hashes that contain more than 2 columns, where the 3rd column will
    > need to be called, usually it's column 1 being the key and the rest of the
    > line being the value, as far as I've seen. Any help, pointers, ideas on
    > where to start, or direction in any way would be appreciated.
    #!perl -w
    use strict;
    my (%ips, %names, data);
    while( <STDIN> ) {
    my ($ip, $fqdn, $class) = split;
    die "Duplicated data" if $ips{$ip} or $names{$fqdn};
    $by_ip{$ip} = $by_name{$fqdn} = 1;
    push data, [$ip, $fqdn, $class]
    }
    my ($new_ip, $new_fqdn, $new_class) = map { split } ARGV;
    if( $ips{$new_ip} ) {
    warn "There already is an ip $new_ip\n";
    } elsif( $names{$new_name} ) {
    warn "There already is a fqdn $new_name\n";
    } else {
    push data, [$new_ip, $new_fqdn, $new_class];
    }
    print "$_\n" foreach sort {
    $$a[2] cmp $$b[2] or $$a[1] cmp $$b[1];
    } data;
    __END__

    Now, run this as:

    perl the_prog.pl newip newname newclass < oldfile > newfile

    And it should add the data in the manner you want.

    --
    $a=24;split//,240513;s/\B/ => /for=qw(ac ab bc ba cb ca
    );{push(b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop b))&&redo;}
    Benjamin Goldberg Guest

  2. #2

    Default Re: search for duplicates and sort array


    "Rick L" <nonenone.com> wrote in message
    news:fVO2b.431$mD.327news.level3.com...
    >
    > What I've done is write up a script for users to be able to add/remove
    > entries from this array. There are two more things that I need to do in
    > order for this to be complete. First, if a user adds an entry, I need to
    > check the 1st or 2nd column to make sure that either the IP or FQDN don't
    > match anything else already listed in the array. I've tried to run this
    in
    > a while loop and if it comes across an entry that exists, exit and print
    an
    > error message, but that isn't working for me. Secondly, once an addition
    to
    > the list is made, I would like to sort the array.
    I leave the explanation of the following code as an exercise to you, and
    optimizations to anyone so inclined:

    use strict;
    use warnings;

    my %hoa;

    my tosort;

    while (my $line = <DATA>) {

    next if $line =~ /^\s*$/;
    my val = split(/\s+/, $line);
    $hoa{$val[0]} = [val];
    push tosort, \val;

    }


    my array1 = ['12.12.12.12', 'blash.com', 'A'];
    my array2 = ['112.112.112.112', 'blah98.com', 'B'];
    my array3 = ['24.34.34.34', 'check.com', 'A'];


    my new_sites;

    SITECHK: foreach my $aref (array1, array2, array3) {

    if ($hoa{{$aref}[0]}) {

    undef {$aref};
    next;

    }

    my $lc_fqdn = lc({$aref}[1]);

    foreach my $key (keys %hoa) {

    if (lc($hoa{$key}[1]) eq $lc_fqdn) {

    undef {$aref};
    next SITECHK;

    }

    }

    push tosort, $aref;

    }

    my sorted = sort { {$a}[2] cmp {$b}[2] ||
    {$a}[1] cmp {$b}[1] } tosort;


    foreach my $aref (sorted) {

    print "{$aref}[2] = {$aref}[1] = {$aref}[0]\n";

    }




    __END__

    12.12.12.12 blah.com A
    34.34.34.34 blah3.com B
    44.44.44.44 check.com C
    23.23.23.23 blah2.com A
    43.43.43.43 check2.com C
    33.33.33.33 blah4.com B



    Matt Garrish Guest

  3. #3

    Default Re: search for duplicates and sort array

    Thanks folks, you all gave some great advice and pointers. For some reason
    I just couldn't visualize what needed to be done and you all gave me what I
    needed to break out of it. I really appreciate the help.


    Rick L Guest

Similar Threads

  1. how do you sort a collection search
    By oak3 in forum Coldfusion - Advanced Techniques
    Replies: 3
    Last Post: March 28th, 06:50 PM
  2. search and sort data
    By frankgrimes webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 3rd, 09:10 PM
  3. Merge sort and binary search
    By ChiehAnLu webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 0
    Last Post: November 11th, 10:56 PM
  4. Replies: 8
    Last Post: August 2nd, 10:46 AM
  5. shows duplicates of every file in search results
    By b. Martino in forum Windows XP/2000/ME
    Replies: 1
    Last Post: July 18th, 03:28 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139