Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 17th, 2000, 06:20 AM
timbo timbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 1999
Location: London
Posts: 110 timbo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 26 sec
Reputation Power: 0
hi

i need to open a file, read a line and then compare that to the line below it, if it matches delete the first and then compare the line after it, basically running through checking each line against the next.
i can open the file, read through it, out put and do the checking, I was doing it it as a for loop (of 2) and then slowly chugging through, but I'm sure there must be a more efficient way of doing this.

any thoughts anyone, help greatly appreciated

tia

t

Reply With Quote
  #2  
Old August 17th, 2000, 07:05 AM
christucker2 christucker2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Posts: 81 christucker2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Simplest way to do it is this:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
use File::Copy;

open INFILE, $myfile or die "Failed to open $myfile for input: $!";
open OUTFILE, ">$myfile.out" or die "Failed to open $myfile.out for output: $!";
my $current = <INFILE>;
while( my $next = <INFILE> ) {
print OUTFILE $current if $current ne $next;
$current = $next;
}
print OUTFILE $current;
close OUTFILE;
close INFILE;
copy( "$myfile.out", $myfile );
[/code]
It's alright efficiency-wise, but has two major problems:
1) It requires enough disk space to write out another copy of the file (without the line duplications)
2) It requires a copy of the file

It also leaves the temporary file there, but deleting that is easy enough.
Neither of these two problems should cause too much of an issue if you're not working with overly larger files, but it could be a problem if you are. You MIGHT be able to overcome these using lower level read/write routines on a single file, but even if you can (which I'm not sure is all that likely), you'll probably have to go through quite a lot of pain to get it to work. At least the way detailed above doesn't require a lot of memory (just enough to hold two lines at a time) so you shouldn't hit problems that way.

Reply With Quote
  #3  
Old August 17th, 2000, 08:34 AM
timbo timbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 1999
Location: London
Posts: 110 timbo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 26 sec
Reputation Power: 0
cheers chris

worked far better than mine did

though you got round the need for duplication by copying the new over the orig, so I changed that to rename so you end up with one file.


thanks again

tim

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > erm a quickie I think

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap