in reply to Re: comparing contents of the file
in thread comparing contents of the file

Thankyou...Hi i have written the code as per your algorithm
#!/usr/bin/perl use strict; use warnings; my %lines; open F1, "File.txt"; while(my $result= <F1>) { $lines{$result}=1; } close(F1); open (OF1, ">match.txt"); open (OF2, ">diff.txt"); open F2,"File2.txt"; while(<F2>) { if ($lines{$_}==1) { print OF1 $lines; $lines++; } els { print OF2 $lines; } } close OF1; close OF2;
and i am getting this error: Global symbol "$lines" requires explicit package name at line.pl line 21,22,26. i am gng throu the tutorials..

Replies are listed 'Best First'.
Re^3: comparing contents of the file
by toolic (Bishop) on Jun 04, 2008 at 15:35 UTC
    You have declared a hash, my %lines;, but then you are trying to use that variable as if it were a scalar in this line:
    print OF1 $lines;

    You probably want:

    print OF1 $lines{$_};

    Same for OF2.

    Also, "els" is a typo: should be "else".

    $lines++; is also a problem.

      thanks..i have added the changes but getting error: Use of uninitialized value in numeric eq (==) at line.pl line 20, <F2> line 2. Use of uninitialized value in print at line.pl line 27, <F2> line 2.
      #!/usr/bin/perl use strict; use warnings; my %lines; open F1, "File.txt"; while(my $result= <F1>) { $lines{$result}=1; } close(F1); open (OF1, ">match.txt"); open (OF2, ">diff.txt"); open F2,"File2.txt"; while(<F2>) { if ($lines{$_} == 1) { print OF1 $lines{$_}; $lines{$_}++; } else { print OF2 $lines{$_}; } } close OF1; close OF2;
        It would be better to check for the existence of a hash key:
        if (exists $lines{$_})

        At this point, I would recommend GrandFather's approach.