Comparing two files

Comparing two files

am 15.01.2008 21:55:56 von clearguy02

Hi folks,

I have two files, one with 1000 lines and another with 600 lines and
both files have the user login ID's and the first file has an extra of
400 user id's that I need to find.

Here is the script I have written:
------------------------------------
open (INPUT1,"fullFile.txt") or die "Cannot open the file: $!";
open (INPUT2,"comapreFile.txt") or die "Cannot open the file: $!";

@array1 = ;
@array2 = ;

foreach $word (@array2)
{
if(!grep /$word/i, @array1)
{
print "$_\n";
}
}
----------------------------------------

I am not able to see the output on the screen. Any one sees where I am
doing wrong?

Thanks
J

Re: Comparing two files

am 15.01.2008 22:03:37 von Martijn Lievaart

On Tue, 15 Jan 2008 12:55:56 -0800, clearguy02 wrote:

> Hi folks,
>
> I have two files, one with 1000 lines and another with 600 lines and
> both files have the user login ID's and the first file has an extra of
> 400 user id's that I need to find.
>
> Here is the script I have written:
> ------------------------------------
> open (INPUT1,"fullFile.txt") or die "Cannot open the file: $!"; open
> (INPUT2,"comapreFile.txt") or die "Cannot open the file: $!";
>
> @array1 = ;
> @array2 = ;
>
> foreach $word (@array2)
> {
> if(!grep /$word/i, @array1)
> {
> print "$_\n";

What value is $_ here? Where is it set?

> }
> }
> ----------------------------------------
>
> I am not able to see the output on the screen. Any one sees where I am
> doing wrong?

You could have found this yourself if you added
use strict;
use warnings;
on the top of your script!

HTH,
M4

Re: Comparing two files

am 15.01.2008 22:09:35 von jurgenex

clearguy02@yahoo.com wrote:
>I have two files, one with 1000 lines and another with 600 lines and
>both files have the user login ID's and the first file has an extra of
>400 user id's that I need to find.

That's not too large to slurp into an array.
And then you can simply apply the FAQ " How do I compute the difference of
two arrays?"

jue

Re: Comparing two files

am 15.01.2008 22:19:04 von someone

clearguy02@yahoo.com wrote:
>
> I have two files, one with 1000 lines and another with 600 lines and
> both files have the user login ID's and the first file has an extra of
> 400 user id's that I need to find.
>
> Here is the script I have written:
> ------------------------------------
> open (INPUT1,"fullFile.txt") or die "Cannot open the file: $!";
> open (INPUT2,"comapreFile.txt") or die "Cannot open the file: $!";
>
> @array1 = ;
> @array2 = ;
>
> foreach $word (@array2)
> {
> if(!grep /$word/i, @array1)
> {
> print "$_\n";
> }
> }
> ----------------------------------------
>
> I am not able to see the output on the screen. Any one sees where I am
> doing wrong?

You probably want to do it like this:

open INPUT2, '<', 'comapreFile.txt'
or die "Cannot open 'comapreFile.txt' $!";

my %compare;
while ( ) {
chomp;
$compare{ lc() } = ();
}
close INPUT2;


open INPUT1, '<', 'fullFile.txt'
or die "Cannot open 'fullFile.txt' $!";

while ( ) {
chomp;
print "$_\n" unless exists $compare{ lc() };
}
close INPUT2;



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: Comparing two files

am 15.01.2008 22:51:38 von clearguy02

On Jan 15, 1:19=A0pm, "John W. Krahn" wrote:
> cleargu...@yahoo.com wrote:
>
> > I have two files, one with 1000 lines and another with 600 lines and
> > both files have the user login ID's and the first file has an extra of
> > 400 user id's that I need to find.
>
> > Here is the script I have written:
> > ------------------------------------
> > open (INPUT1,"fullFile.txt") or die "Cannot open the file: $!";
> > open (INPUT2,"comapreFile.txt") or die "Cannot open the file: $!";
>
> > @array1 =3D ;
> > @array2 =3D ;
>
> > foreach $word (@array2)
> > =A0{
> > =A0 =A0if(!grep /$word/i, @array1)
> > =A0 =A0 =A0{
> > =A0 =A0 =A0 =A0print "$_\n";
> > =A0 =A0 =A0}
> > =A0}
> > ----------------------------------------
>
> > I am not able to see the output on the screen. Any one sees where I am
> > doing wrong?
>
> You probably want to do it like this:
>
> open INPUT2, '<', 'comapreFile.txt'
> =A0 =A0 =A0or die "Cannot open 'comapreFile.txt' $!";
>
> my %compare;
> while ( ) {
> =A0 =A0 =A0chomp;
> =A0 =A0 =A0$compare{ lc() } =3D ();
> =A0 =A0 =A0}
> close INPUT2;
>
> open INPUT1, '<', 'fullFile.txt'
> =A0 =A0 =A0or die "Cannot open 'fullFile.txt' $!";
>
> while ( ) {
> =A0 =A0 =A0chomp;
> =A0 =A0 =A0print "$_\n" unless exists $compare{ lc() };
> =A0 =A0 =A0}
> close INPUT2;
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- =
Larry Wall- Hide quoted text -
>
> - Show quoted text -

Thanks to all... I forgot to replace $_ with $word in the snippet.

J