best practices - compare lists

Discussion in 'Perl Misc' started by Rocky Allen, Sep 20, 2005.

  1. Rocky Allen

    Rocky Allen Guest

    Hey Y'all
    I have been trying to find the most effective way to compare the values in
    two arrays. The idea is to match the list of files sent into cpio vs. the
    files that actually made it onto the tape. I have been looking at
    Array::Compare, but the example in cpan is broken. Does anyone know the
    best way to compare two arrays?

    thanks,
    Rocky

    #!/usr/bin/perl

    use strict;
    use warnings;


    my @posttape;
    open FH3, '</tmp/cpioin' or die "Cannot open /tmp/cpioin: $!";
    my @pretape = <FH3>;

    open IN, '<', '/tmp/filesontape' or die "Cannot open /tmp/filesontape: $!";
    while ( <IN> ) {
    my @fields = split;
    push @posttape, "$fields[8]\n";
    }
    close IN;
    open(FH5, '>/tmp/ontape') or die "no FH5:$!";
    print FH5 @posttape;
     
    Rocky Allen, Sep 20, 2005
    #1
    1. Advertising

  2. Rocky Allen

    Guest

    Rocky Allen <> wrote:
    > Hey Y'all
    > I have been trying to find the most effective way to compare the values
    > in two arrays.


    The easiest way to compare two arrays is to convert them into two hashes.


    > The idea is to match the list of files sent into cpio vs.
    > the files that actually made it onto the tape. I have been looking at
    > Array::Compare, but the example in cpan is broken.


    Could you be more specific?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Sep 20, 2005
    #2
    1. Advertising

  3. Rocky Allen

    Paul Lalli Guest

    Rocky Allen wrote:
    > I have been trying to find the most effective way to compare the values in
    > two arrays.


    Have you read
    perldoc -q "compute the difference"
    ?

    > The idea is to match the list of files sent into cpio vs. the
    > files that actually made it onto the tape. I have been looking at
    > Array::Compare, but the example in cpan is broken.


    Yes, but it's not exactly a difficult syntax error to repair. (The
    author forgot a closing ) on his if statement).
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Array::Compare;

    my @arr1 = 0 .. 10;
    my @arr2 = 0 .. 10;

    my $comp = Array::Compare->new;

    if ($comp->compare(\@arr1, \@arr2)) {
    print "Arrays are the same\n";
    } else {
    print "Arrays are different\n";
    }
    __END__
    Arrays are the same

    > Does anyone know the best way to compare two arrays?


    Generally, the way suggested by the FAQ. In this case, you're looking
    for the @difference to be an empty array.

    Paul Lalli
     
    Paul Lalli, Sep 20, 2005
    #3
  4. Rocky Allen wrote:
    > Does anyone know the best way to compare two arrays?


    Simple.

    my $equal=1;
    for (0..$#arr1) {
    $equal = $arr1[$_] == $arr2[$_];
    #use "eq" instead of "==" if comparing text
    last unless $equal;
    }

    There isn't much optimization you can do because you have to compare every
    single element.
     
    Jürgen Exner, Sep 20, 2005
    #4
  5. Rocky Allen wrote:
    > Hey Y'all
    > I have been trying to find the most effective way to compare the values in
    > two arrays. The idea is to match the list of files sent into cpio vs. the
    > files that actually made it onto the tape. I have been looking at
    > Array::Compare, but the example in cpan is broken. Does anyone know the
    > best way to compare two arrays?
    >
    > thanks,
    > Rocky
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    >
    > my @posttape;
    > open FH3, '</tmp/cpioin' or die "Cannot open /tmp/cpioin: $!";
    > my @pretape = <FH3>;
    >
    > open IN, '<', '/tmp/filesontape' or die "Cannot open /tmp/filesontape: $!";
    > while ( <IN> ) {
    > my @fields = split;
    > push @posttape, "$fields[8]\n";
    > }
    > close IN;
    > open(FH5, '>/tmp/ontape') or die "no FH5:$!";
    > print FH5 @posttape;


    In Ruby:

    p pretape - posttape
     
    William James, Sep 20, 2005
    #5
  6. Rocky Allen

    Jeff Guest

    William James wrote:
    > In Ruby:
    >
    > p pretape - posttape


    (To the tune of "Pop goes the weasel")

    All around the perl newsgroup
    William showed off Ruby
    William thought it all was in fun
    *plonk* goes the lame troll
     
    Jeff, Sep 20, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    433
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. Esmail
    Replies:
    0
    Views:
    284
    Esmail
    Apr 24, 2009
  3. Esmail
    Replies:
    3
    Views:
    399
  4. Esmail
    Replies:
    6
    Views:
    283
    Terry Reedy
    Apr 24, 2009
  5. norseman
    Replies:
    3
    Views:
    1,082
    ajaksu
    Apr 24, 2009
Loading...

Share This Page