Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing two arrays 1

Status
Not open for further replies.

lmbylsma

Programmer
Nov 11, 2003
33
US
What I want to do is compare two arrays, one is just a vector with a single column, the other is multidimensional. I want to look at the 3 column in the multidimensional array and compare it to the single column in the other array and remove all of the lines from the multidimensional array for which those values match up.

For example, my two arrays look something like this:

array1:

2 5.53262 5.19032 342.3 2
2 5.56062 5.21615 344.47 2
2 5.5947 5.28492 309.78 4
2 5.7783 5.43707 341.23 2
2 5.78695 5.44332 343.63 7

array2:

1.13855
1.78910
5.21615
8.52778
10.34950
17.05255
17.33832


So in this example, since the value 5.21615 is in array 2 and column 3 from array 1, I want that line deleted from array1, so the result would be:

2 5.53262 5.19032 342.3 2
2 5.5947 5.28492 309.78 4
2 5.7783 5.43707 341.23 2
2 5.78695 5.44332 343.63 7

It can either be deleted and print the resulting array or tell it to print lines only if that value is not identical to array2.

Here's the code I have sofar, all it does not is read two different files to make the two arrays.

#!/usr/bin/perl -w

my @array1;
my @array2;

my $datafile=<alldata.txt>;
my $datafile2=<tworesponses.txt>;

while (<DATA>) {
chomp;
push @array1, [split/\t/];
}

close(DATA);

open(DATA2, $datafile2) || die qq(Can't open "$datafile2" for input\n);

while (<DATA2>) {
chomp;
push @array2, $_;

}

close(DATA2);
 
something along these lines should work (not tested):
Code:
#!/usr/bin/perl -w
use strict;

my @array1 = ();
my $datafile = 'alldata.txt';
my $datafile2 = 'tworesponses.txt';

open(DATA2, $datafile2) || die qq(Can't open "$datafile2" for input\n);
my @array2 = <DATA2>;
close(DATA2);
chomp(@array2);

open(DATA, $datafile) || die qq(Can't open "$datafile" for input\n);
OUTERLOOP: while (<DATA>) {
    my $tmp = (split(/\t/,$_))[2]; #get third column only
    for my $i (0 .. $#array2) {
      next OUTERLOOP if $array2[$i] == $tmp;
    }
    push @array1, $_;
}
close(DATA);

print "$_\n" for @array1;
 
An alternate strategy that makes the search list into a hash and uses grep with key lookup:
Code:
#!perl
use strict;
use warnings;

open(FH1, "ibs4a.txt") || die qq(Can't open "ibs4a.txt" for input!\n);
open(FH2, "ibs4b.txt") || die qq(Can't open "ibs4b.txt" for input!\n);

my @arr1 = map {chomp; [split]} <FH1>;
my %h1 = map {chomp; $_ => undef} <FH2>;

close(FH1) || die qq(Can't open "ibs4a.txt" after read!\n);
close(FH2) || die qq(Can't open "ibs4b.txt" after read!\n);

my @arr2 = grep {!exists($h1{$_->[2]})} @arr1;
print join("\t", @$_), "\n" for @arr2;
Output:
Code:
2       5.53262 5.19032 342.3   2
2       5.5947  5.28492 309.78  4
2       5.7783  5.43707 341.23  2
2       5.78695 5.44332 343.63  7

 
Thanks so much, both of those suggestions work great.
 
I'm not that good in programming Perl,
but here is how I'd do it in Ruby:

Code:
#!/usr/bin/ruby

arr1 = []
arr2 = []

begin
    File.open('alldata.txt') { |f|
        f.each_line { |line| arr1 << line.split(/\s+/) }
    }

    File.open('tworesponses.txt') { |f|
        f.each_line { |line| arr2 << line.chomp }
    }

    arr1.each { |a|
        puts a.join("\t") unless arr2.include? a[2]
    }
rescue SystemCallError
    STDERR.puts $!
end
Output:
Code:
2       5.53262 5.19032 342.3   2
2       5.5947  5.28492 309.78  4
2       5.7783  5.43707 341.23  2
2       5.78695 5.44332 343.63  7
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top