LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-25-2001, 08:57 AM   #1
Paul_Lee
Member
 
Registered: Jul 2001
Location: Cambridgeshire, England
Distribution: Ubuntu 13.10
Posts: 93

Rep: Reputation: 15
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
 
Old 08-25-2001, 09:33 AM   #2
DoubleLetter
Member
 
Registered: Jun 2001
Location: Sharjah, United Arab Emirates
Distribution: Mandrake Linux 10.1
Posts: 132

Rep: Reputation: 15
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
 
Old 08-25-2001, 09:36 AM   #3
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
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...
 
Old 08-25-2001, 11:33 AM   #4
Paul_Lee
Member
 
Registered: Jul 2001
Location: Cambridgeshire, England
Distribution: Ubuntu 13.10
Posts: 93

Original Poster
Rep: Reputation: 15
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
 
Old 08-25-2001, 12:35 PM   #5
Paul_Lee
Member
 
Registered: Jul 2001
Location: Cambridgeshire, England
Distribution: Ubuntu 13.10
Posts: 93

Original Poster
Rep: Reputation: 15
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!
 
Old 08-25-2001, 02:53 PM   #6
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
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...
 
Old 08-25-2001, 06:28 PM   #7
Paul_Lee
Member
 
Registered: Jul 2001
Location: Cambridgeshire, England
Distribution: Ubuntu 13.10
Posts: 93

Original Poster
Rep: Reputation: 15
(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
 
Old 08-25-2001, 06:46 PM   #8
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Driver to read/write to MS Access DB w/Perl bpotts Linux - General 1 09-15-2005 10:24 PM
Perl/Tk Web Access Botton petebatt Programming 0 05-11-2005 04:46 PM
Perl file conversion results in a truncated file kshaffer Programming 0 01-31-2005 03:31 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 06:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM


All times are GMT -5. The time now is 08:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration