|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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... |
|
#2
|
||||
|
||||
|
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).] |
|
#3
|
|||
|
|||
|
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... |
|
#4
|
|||
|
|||
|
# 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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > edititng a file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|