Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > comparing some ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 10688 of 12002
Post > Topic >>

comparing some but not all fields in lists

by dnewman@[EMAIL PROTECTED] (David Newman) Mar 3, 2008 at 02:32 PM

Greetings. I'm looking to compare two contact lists in csv format, and 
then print out "here are the records in in Llist only, in Rlist only, 
and what's in common."

I should compare only 3 of the 82 fields in each list. There are 
differences in some of the other fields that I should ignore.

If I read in each csv file as an array, List::Compare does a nice job of 
comparing all 82 fields as a single array element. But I should only 
look at 3 fields, not all 82. (snippet A below)

I can also use List::Compare plus a split function to strip out just the 
3 fields I'm comparing. However, the resuling arrays then only have 
three fields in each array element. (snippet B below)

How to compare only selected fields in each list, but then present all 
fields for any matches?

thanks

dn


SNIPPET A

open(DAT, $Rfile) or die("unable to open");

# here, each line has 82 fields
my @[EMAIL PROTECTED]
 = <DAT>;

}

close(DAT);

# yes, it's stupid that this repeats
open(DAT, $Rfile) or die("unable to open");

my @[EMAIL PROTECTED]
 = <DAT>;

}

close(DAT);

$lc = List::Compare->new(\@[EMAIL PROTECTED]
 \@[EMAIL PROTECTED]
);
@[EMAIL PROTECTED]
 = $lc->get_unique;
@[EMAIL PROTECTED]
 = $lc->get_complement;
@[EMAIL PROTECTED]
 = $lc->get_intersection;

# each line still has 82 fields


SNIPPET B

open(DAT, $Lfile) or die("unable to open");

foreach $line (<DAT>) {

	($FirstName,
	$LastName,
	$Company) = split ',', $line;
	$compCriteria = $FirstName . "," . $LastName . "," . $Company;
	# here, each line has 1 field, concatenated from 3
	push (@[EMAIL PROTECTED]
 $compCriteria);

}
close(DAT);

# get files
open(DAT, $Rfile) or die("unable to open");

foreach $line (<DAT>) {

	($FirstName,
	$LastName,
	$Company) = split ',', $line;
	$compCriteria = $FirstName . "," . $LastName . "," . $Company;
	push (@[EMAIL PROTECTED]
 $compCriteria);

}
close(DAT);

# find and print common entries

$lc = List::Compare->new(\@[EMAIL PROTECTED]
 \@[EMAIL PROTECTED]
);
@[EMAIL PROTECTED]
 = $lc->get_unique;
@[EMAIL PROTECTED]
 = $lc->get_complement;
@[EMAIL PROTECTED]
 = $lc->get_intersection;

# each line has 1 field, concatenated from 3
 




 7 Posts in Topic:
comparing some but not all fields in lists
dnewman@[EMAIL PROTECTED]  2008-03-03 14:32:42 
Re: comparing some but not all fields in lists
noreply@[EMAIL PROTECTED]  2008-03-04 01:26:21 
Re: comparing some but not all fields in lists
merlyn@[EMAIL PROTECTED]   2008-03-03 19:55:07 
Re: comparing some but not all fields in lists
noreply@[EMAIL PROTECTED]  2008-03-04 09:52:41 
Re: comparing some but not all fields in lists
krahnj@[EMAIL PROTECTED]   2008-03-03 22:05:18 
Re: comparing some but not all fields in lists
daggerquill@[EMAIL PROTEC  2008-03-04 17:38:59 
Re: comparing some but not all fields in lists
dnewman@[EMAIL PROTECTED]  2008-03-16 17:14:25 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 9:28:30 CDT 2008.