Update a Line and Then Replace the Old One
Hi everyone,
I have a script which searches through a file of lines. If it finds a match, according to my criteria, I add a couple of elements to the line, and then return it to the file. So instead of the line being:
field1|field2|field3
it becomes
field1|field2|field3|field4|field5
Which is great, except that I also need to delete the initial line (with only 3 fields) from the file. Problem is, I don't know what line number it is. The file could have any number of lines.
How can I do this? So that I end up with only the modified line in the file? In other words, the file would still have the same number of lines, but the "matched" line would be appended...
Thanks.
Mark Hensler posted this at 01:27 — 28th October 2001.
He has: 4,048 posts
Joined: Aug 2000
How did you find that line in the first place?
Loop through the file saving every line in an array except for the line that matches your previous criteria. Then write the array to the file.
Mark Hensler
If there is no answer on Google, then there is no question.
rline posted this at 02:43 — 28th October 2001.
They have: 40 posts
Joined: Oct 2001
Yeah I know. Sounds like it should be a piece of cake, but as ususal, not quite. Maybe I'll explain more what I'm really trying to do, and you might know a really incredibly simple way of doing it, which bypasses all this...
It's a script which looks at a file of properties available, as well as a file of property requirements that people have sent in via a form. So when this script runs, if it finds an available property, which has enough "mathces" with what the client wants in a property, it's a match, and the script then sends an email to the client telling them that there's a property they might be interested in. I can run the script as many or as few times as I want. But what I don't want is for the client to get more than one email about the same available property. In other words, once a match takes place, I need to do something, so that if I run the script one minute later, it notices that that combination of client and available property has already been matched, and therefore bypasses it.
Does that make sense?
That's all I'm trying to do...and I'm probably making it a lot harder than it needs to be.
Mark Hensler posted this at 07:15 — 28th October 2001.
He has: 4,048 posts
Joined: Aug 2000
I would read the file into an array. Then loop through the array, looking for your criteria. Once found, send your email, then alter that element in the array (thus altering the line in the file). Once you've exited the loop, loop through the same array again, this time writting the array over top the file.
pseudo-code:
# suck the file into an array
fopen(FILE, "<my_file.ext");
@file = <FILE>;
close(FILE);
# loop through lines
foreach (@file as $line) {
# run your criteria checks
# this is a mock.. I have no idea how this will look
if ($property eq $client_criteria) {
# mail the client
#( I forgot how 8P )
# add a flag to the line
$line .= "|$client_email";
}
}
# write over the old file with your newly altered lines
fopen(FILE,">my_file.ext");
# loop through lines
foreach (@file as $line) {
print FILE $line;
}
close(FILE);
You may want a file lock, depends on how frequently the file will be accessed.
Also, I'm not sure if you need to print a "\n" after $line when reqwriting the file. I think each PRINT will make a new line, but if not add a "\n" to it.
Hope that helps,
Mark Hensler
If there is no answer on Google, then there is no question.
rline posted this at 08:36 — 29th October 2001.
They have: 40 posts
Joined: Oct 2001
Thanks heaps Mark,
It ALMOST works! The only thing it's doing wrong is this:
$line .= "|$client_email\n";
It's writing $client_email onto the next line, instead of adding it to the end of the $line. I really don't know why. So if the old file had two lines, and line 1 gets a match, here's how it looks:
OLD:
field1|field2|field3
field1|field2|field3 # both these are distinct lines
NEW:
field1|field2|field3
client email
field1|field2|field3
As you can see, client email is meant to hang on the end of the first line, but it ain't...If this can be fixed, it does the job perfectly...Any thoughts?
Thanks
Mark Hensler posted this at 17:03 — 29th October 2001.
He has: 4,048 posts
Joined: Aug 2000
try this:
chop($line);
$line .= "|$client_email\n";
Or maybe it's chomp()... play with it and let me know.
Docs: chomp(), chop()
rline posted this at 23:34 — 29th October 2001.
They have: 40 posts
Joined: Oct 2001
Thanks Mark,
works perfectly!! Thanks for your help...
(It was chomp...)
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.