On Jan 22, 2004, at 4:52 PM, [EMAIL PROTECTED] wrote:
This very green newbie would like to compare two files, let's say File1 and File2. I want to put the difference from File2 only, into a new file, File3.
For example:
File1.txt oranges apples bananas
File2.txt apples kiwi bananas
The result I want for File3 is the new entry in File2, which is kiwi. (I
don't care that oranges was in File1 and not File2.)
in theory then file2.txt could have been
oranges apples kiwi banana
what about apples kiwi wombat bananas
you would want to have kiwi and wombat
One strategy would be say: my @file1 = qw(oranges apples bananas); my @file2 = qw(apples kiwi bananas frodo bagins); my @list = get_diff_list([EMAIL PROTECTED],[EMAIL PROTECTED]); print "we see $_\n" foreach(@list); #------------------------ # sub get_diff_list { my ($list1, $list2 ) = @_; my %hash = map { $_ => 1 } @$list1; my @ret_list; foreach (@$list2) { if ( $hash{$_} ) { delete( $hash{$_} ); } else { push(@ret_list,$_); } } # if you wanted to have the remaining bits that were # in list1 and not in list2 #push(@ret_list,$_) foreach(keys(%hash)); @ret_list; } # end of get_diff_list
or how about
sub get_diff_list { my ($list1, $list2 ) = @_; my %hash = map { $_ => 1 } @$list1; grep { $_ if ( ! exists($hash{$_})) } @$list2; }
ciao drieux
---
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>