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 |
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 |
Sounds to me like you are reinventing the wheel! Have a look at
Code:
man diff cheers Jamie... |
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 |
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! ;)
|
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"; Jamie... |
(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 |
How about something like this
Code:
$file1 = "someFile.txt"; Jamie... |
All times are GMT -5. The time now is 04:10 AM. |