nascar_3
asked on
replace }else{ append to text file...?
I'm trying to read a text file, each line being a record with a unique name at the beginning, and comparing to a new input line. Based on whether the name of the record finds a match or not, either replace the matching line, otherwise append that line to the end of the file. Also to delete a line if indicated.
I've played with some code I found, but the results are either it duplicates the lines read in and appends them all, or it just writes the same (new) line out for every record.
I won't muck things up with the code I've started with, I want a fresh start, but basically it needs to look like:
file content example:
name=value|value|value|val ue|...cont inues....
monkey=something|anotherth ing|anythi ng|....... .
bear=problem|anything|many things|dup licate|tre e|.......
harvey=speaker|duplicate|s omething|. .......
$saveline = $ENV{QUERY_STRING} # new line input
open the existing file.
read each line already there.
compare 'name' value of $saveline to file records.
if names match AND value='delete' kill this record.
}elsif{
names match AND value!='delete' replace the older record with the new one.
}else{
leave the older record as-is.
}
write all records back out to the file.
close file.
So (using data above) if $saveline is:
buick=old|blue|something|q uite|carpe t|........
it would append as a new record.
But if $saveline is:
bear=pink|orange|blue|gree n|red|.... ..
it would overwrite the existing 'bear' line.
Or if $saveline is:
bear=delete|
this record is removed from the file.
Hope I'm making sense here, sorry for the weird thumbnail layout, but I'm in a hurry, I'm at work and have no reference material in front of me.
thanks!
I've played with some code I found, but the results are either it duplicates the lines read in and appends them all, or it just writes the same (new) line out for every record.
I won't muck things up with the code I've started with, I want a fresh start, but basically it needs to look like:
file content example:
name=value|value|value|val
monkey=something|anotherth
bear=problem|anything|many
harvey=speaker|duplicate|s
$saveline = $ENV{QUERY_STRING} # new line input
open the existing file.
read each line already there.
compare 'name' value of $saveline to file records.
if names match AND value='delete' kill this record.
}elsif{
names match AND value!='delete' replace the older record with the new one.
}else{
leave the older record as-is.
}
write all records back out to the file.
close file.
So (using data above) if $saveline is:
buick=old|blue|something|q
it would append as a new record.
But if $saveline is:
bear=pink|orange|blue|gree
it would overwrite the existing 'bear' line.
Or if $saveline is:
bear=delete|
this record is removed from the file.
Hope I'm making sense here, sorry for the weird thumbnail layout, but I'm in a hurry, I'm at work and have no reference material in front of me.
thanks!
Does the order of the lines in the file matter?
ASKER
No, there is nothing depending on the sort order.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#sorry, that should have been
($name,$value) = (split/[=|]/,$saveline)[0, 1];
($name,$value) = (split/[=|]/,$saveline)[0,
ASKER
thanks ozo-
I'll play with this later when I'm home, but a couple of questions:
Could you explain a little bit about what these 2 lines are doing?
chomp $saveline;
$saveline .= $/;
Why the []and | in the split function?
I would have thought just (split/=/,$saveline)
($name,$value) = (split/[=|]/,$saveline)[0, 1];
I'll play with this later when I'm home, but a couple of questions:
Could you explain a little bit about what these 2 lines are doing?
chomp $saveline;
$saveline .= $/;
Why the []and | in the split function?
I would have thought just (split/=/,$saveline)
($name,$value) = (split/[=|]/,$saveline)[0,
chomp $saveline;
$saveline .= $/;
#I wanted to make sure $saveline ended with one input record separator
split/=/,$saveline would be fine, except that you then have to compare $value eq "delete|\n"
$saveline .= $/;
#I wanted to make sure $saveline ended with one input record separator
split/=/,$saveline would be fine, except that you then have to compare $value eq "delete|\n"
ASKER
Thanks ozo!
I pretty much just plugged it in and it worked!
I needed to add : $saveline=~ s/%20/ /g;
because my form was passing the spaces as '%20', but then it performed just as I wanted it to.
Much Appreciated!
I pretty much just plugged it in and it worked!
I needed to add : $saveline=~ s/%20/ /g;
because my form was passing the spaces as '%20', but then it performed just as I wanted it to.
Much Appreciated!