best practices - compare lists

R

Rocky Allen

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;
 
X

xhoster

Rocky Allen said:
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
 
P

Paul Lalli

Rocky said:
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
 
J

Jürgen Exner

Rocky said:
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.
 
W

William James

Rocky said:
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
 
J

Jeff

William said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top