Thank you Dan and Wolf ! With the suggested changes, my foreach loop
script now works as I hoped it would. (My first script did have a typo,
as you pointed out, though my logic was still wrong.) I'm glad to be able
to set aside my study of hashes for another day. I needed to get this
problem solved so that I can get some other work done.
To correct the script, I added LABELS, used the next statement with a
LABEL, and moved the $print statement out of the inner loop, and waalaa,
it worked properly. I quickly went over the logic of the working program
and it makes sense. It's funny how things seem so clear once they're
solved !
My files are actually probably only 30 to 35 lines each, so size isn't a
problem. The real data I'm comparing has email addresses in them. File2
will either match File1, or have new email addresses in them. I then do
stuff with the new email addresses.
Thanks again. I really appreciate the help.
Here's the working program:
use strict;
use warnings;
my $file1;
my $file2;
my @file1 = qw(oranges apples bananas);
my @file2 = qw(apples kiwi bananas);
FILE2: foreach $file2 (@file2){
FILE1: foreach $file1 (@file1){
if ("$file2" eq "$file1") {
next FILE2;
}
}
print "$file2 \n";
}
The output is "kiwi", which is exactly right.
kiwi
wolf blaum <[EMAIL PROTECTED]>
01/22/2004 08:38 PM
To
[EMAIL PROTECTED], [EMAIL PROTECTED]
cc
Subject
Re: Need help comparing lines in two files
> 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.
I had a very simliar problem about a week ago, which James answerd here:
http://groups.google.com/groups?q=Perl+looping+(a+lot+of)
+files&hl=en&lr=&ie=UTF-8&selm=28A16704-4AD3-11D8-9A03-000A95BA45F8%
40grayproductions.net&rnum=1
or try google groups "perl looping through (a lot of) files"
The only really difference is that I didnt want to compare one FILE2 to
FILE1
but 500.
However, be carefull on your filesize:
I settled reading one file into mem (as an array) and looping through the
other ones using a while (<FILE2>) reading the 500 files line by line.
> 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.)
>
> I tried using a nested foreach loop structure, but I can't get that to
> work and I have a feeling using nested foreach's is not the way to go.
why not?
> I'm guessing somehow I should use hashs, but I've never used a hash for
> anything and I don't really know how to use a hash. Can someone help ?
do you need to associate the contens of the line with a filename ore
something? if not, use an array.
> Here's my feeble attempt:
>
> my $file1;
> my $file2;
>
> my @file1 = qw(oranges apples bananas);
> my @file2 = qw(apples kiwi bananas);
>
As Dan showed:
FILE2:
> foreach $file2 (@file2){
> foreach $file2 (@file2){
you may want
foreach my $file1(@file1){
here
> #print "$mastervob $tempvob \n";
> if ($file2 eq $file1) {
> last; # I would like to go up to the
> toplevel "foreach" here, but I don't know how to do it
> } # and I'm not sure this would
even
> work.
as Dan said:
next FILE2;
will do the job.
> else{
> print "$file2 \n";
> }
> }
> }
This doesent do what I assume you want: when you place the print in the
inner
loop.
Just look at the link above.
Hope thats a start, Wolf