Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old May 21st, 2000, 04:10 PM
tucats tucats is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 1999
Posts: 18 tucats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello... i am brand new to perl...

How do i edit a file using perl...?

example:

my file has 4 lines (4 entries)... i know how to append to the file and how to overwrite the file...

but how do i change just one of the lines...?

thank you for any help...

Reply With Quote
  #2  
Old May 22nd, 2000, 02:22 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 9
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan


i would suggest you a logic for modifying a record in a flatfile..

first call the row on the screen for editing.

When user clicks on modify button.you should write all the records to a temporary file except the row which you want to modify.

and rename the temporary file to your orginal file name.

And finally append your modified value to the orginal file..


ie,

let us say ,

we have a file called test.txt .and it is having following records

sno|Name
--------
1|shiju
2|thomas
3|rajesh
4|james


i want to edit the sno 3 and change the name value to PETER THOMAS.
i should do the following for modifying the sno 3.


#!/usr/bin/perl


open(TEMP,">temp.txt") | | die "Error creating a file $!n";

#open a temperary file for storing the data from test.txt

open(DB,"test.txt") | | die "Error opening database $!n";
#open test.txt file

while(<DB> ){
chomp;
($sno,$name)=split(/|/,$_);

print TEMP "$_n" if $sno!="3";
#print the records to the temp.txt except sno 3
}

unlink("test.txt");
#delete the test.txt
rename("temp.txt","test.txt");

#rename the temp.txt to test.txt

#now 3rd row has been deleted from the database.

close(DB);
close(TEMP);


#now just appned the 3rd row to the test.txt


open(DB,">>test.txt") | | die "Error open database $!n";
#open database in append mode..

print DB "3|PETER THOMASn";

close(DB);

###########------#########--########

just modify the above script as per your requirment.I have not tested this script.but it should work fine...


Good Luck!!

------------------
SR -
shiju.dreamcenter.net

"The fear of the LORD is the beginning of knowledge..."

[This message has been edited by Shiju Rajan (edited May 22, 2000).]

Reply With Quote
  #3  
Old May 22nd, 2000, 03:03 AM
tucats tucats is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 1999
Posts: 18 tucats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Actually, i was doing something very similar already...

my input to the file comes from a string query... so what i did is write the string query to a "temporary" file and then assign the file to a variable (that way i still have the query saved in a file)

i then open my other file and s/$number[0]/$adjust... and then overwrite that file with the "adjusted" result @number...

Something is not right... it doesn't work... my variables are assigned what they are supposed to be: $number[0] and $adjust... but either they are not "substituted" in @number or they are not written to my file...

here is that part of the script:

######################################
# Subroutine adjust file
######################################

sub adjust
{

$tempor="cgi-bin/temporary/$ENV{'REMOTE_ADDR'}.txt";

open TEMPOR, ">$tempor" or die "Cannot open $tempor for write :$!";

$temp=<STDIN>;

print TEMPOR "$tempn";

close TEMPOR;

open TEMPOR, "$tempor" or die "Cannot open $tempor for write :$!";

$adjust=<TEMPOR>;

close TEMPOR;
}

######################################
# subrouting write new file
######################################

sub writefile
{

$out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";

open OUT, "$out" or die "Cannot open $out for write :$!";

@number = <OUT>;

close OUT;

open OUT, ">$out" or die "Cannot open $out for write :$!";

s/$number[0]/$adjust/;

print OUT "@number";

close OUT;


}

HELP...

Reply With Quote
  #4  
Old May 22nd, 2000, 12:19 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
# This line at the end
s/$number[0]/$adjust/; # this method is not allowed

Luckily, you have assigned a $number associate with its string, so the result could always be sorted as 1,2,3,4 instead of 1,2,4,3.

Anyway, here is my suggestion..
############################################
#!/usr/local/bin/perl

# Be sure to chmod 777 to /order
# and 666 to $ENV{'REMOTE_ADDR'}.txt
$out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";

# you need to add a 'Parse Form' sub here yourself
open(OUT,"$out");
@lines = <OUT>;
close (OUT);
$count =0;
foreach $field (@lines) {
($num,$ip) = split(/|/, $field);
if ($num == "$FORM{'num'}") {
$num = $1;
last;
}
else {
$count++;
}
}
splice (@lines, $count, 1);
open(OUT,">$out");
foreach $field (@lines) {
print OUT $field;
}
open(OUT,">>$out");
flock (OUT, 2);
print OUT "$FORM{'num'}|$FORM{'ip'}n";
close(OUT);
# you need to add a 'Return html page" sub yourself



Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > edititng a file


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway