LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   File access in Perl (https://www.linuxquestions.org/questions/programming-9/file-access-in-perl-5840/)

Paul_Lee 08-25-2001 07:57 AM

File access in Perl
 
Hi all,
I've been given the task of comparing two text files and outputting a consolidating the differences into a third text file. Perl seems to be the best way to do this. However, I am a bit
curious about a few Perl aspects:

I'm planning on opening the first file, and for each line, run
through the second file. So, I'm going to put the compare
function inside some form of loop. I can either do this by
opening the 2nd file for each loop, and then close it at the
end, or by repositioning the file pointer to the start of the
2nd file at the end of each loop. Alternately, I could load the whole of the 2nd file into some form of global variable...which one
of these approaches is faster? (Both files are ~100 kbytes long)?

What is the Perl syntax to load a whole file into a variable, as in the second solution? Or, if I decide to do the first approach, what is the syntax to reposition the file pointer? I've had a look at the documentation, and it seems that seek is the command to use, but the books warn against using it because it requires an offset value, which "makes it hard to use". Or so they say.

Which approach would be better?

Cheers!

Paul

DoubleLetter 08-25-2001 08:33 AM

Python
 
If you're a newbie to programming, try Python.

It's much more straightforward. Simple tasks such as these have a low learning curve.

http://www.python.org

jharris 08-25-2001 08:36 AM

Sounds to me like you are reinventing the wheel! Have a look at
Code:

man diff
If this doesn't do what you want then post back and I'll go through your original questions!

cheers

Jamie...

Paul_Lee 08-25-2001 10:33 AM

Thanks........but alas, it wouldn't work because
a) I'm doing this on a Windoze platform (yuk, you've made me mention the dreaded W* word now!), and Perl is a hell of a lot
better than writing an awful VB macro
and
b) the order of entries in the table might not be necessarily the same- I'm just looking for matching entires).

Cheers

Paul

Paul_Lee 08-25-2001 11:35 AM

Re: python
 
Thanks for the advice (though, I'm not a newbie programmer, just a newbie at Perl!); sadly, its Perl or bust. It took work long enough to agree to install Perl, and I simply can't waste time while they prance around thinking about Python! ;)

jharris 08-25-2001 01:53 PM

If I were you I would read both of the files into seperate arrays, then output the differences to the third file. To read the files into an array use the following syntax
Code:

$file1 = "someFile.txt";
$file2 = "someOtherFile.txt";
$file3 = "outputFile.txt";

#open the first file and read it into an array
open(FILE1,$file1);
firstArray = (<FILE1>);
close(FILE1);

#do the same for the second file
open(FILE2, $file2);
secondArray = (<FILE2>);
close(FILE2);

#open the third file for output
open(FILE3, "> $file3");

<<someloop>>
If there isn't a matching line in the same position in the array {
print FILE3 $line;
}
<<end some loop>>
close(FILE3);

HTH

Jamie...

Paul_Lee 08-25-2001 05:28 PM

(I did post this before, but it seems to have been eaten up somewhere).

Thanks for your suggestions; they look good! The only problem is the line saying

>If there isn't a matching line in the same position in the array

This doesn't really apply in this case since I am doing a field-by-field search rather than a line by line search. For instance:

in file1, I may have
1 2 Fn1
3 4 Fn2
5 6 Fn3

and in file 2 I might have
7 8 Fn3
9 10 Fn1
11 12 Fn2

In the consolidated output file, I would have information based, on a line per line basis, on Fn1, Fn2, Fn3 etc., regardless of the order that they were placed in either file.

Thanks though - I can probably salvage something!

Cheers

Paul

jharris 08-25-2001 05:46 PM

How about something like this
Code:

$file1 = "someFile.txt";
$file2 = "someOtherFile.txt";
$file3 = "outputFile.txt";

#open the first file and read it into an array
open(FILE1,$file1);
firstArray = (<FILE1> );
close(FILE1);

#do the same for the second file
open(FILE2, $file2);
secondArray = (<FILE2> );
close(FILE2);

#open the third file for output
open(FILE3, "> $file3");

foreach $line1 (@firstArray) {
    foreach $line2 (@secondArray) {
        #if a matching line is found then jump back out into the first foreach loop, otherwise print the line as one that isn't in both files
        if ($line1 eq $line2) last;
        print FILE3 $line1;
    }
}

close(FILE3);

HTH

Jamie...


All times are GMT -5. The time now is 04:10 AM.