Say if I have a list of names in a text file, like this (Bob, Bill, Steve, Sam, ) and I want to delete "Bill, ". How do I do that?
Jul 27, 2002, 7:56 AM
New User (4 posts)
Jul 27, 2002, 7:56 AM
Post #3 of 7
Views: 5109
Oh, I dunno. It's for a chatbot I'm making--a friendslist, to be precise. I want to add and delete names on the list with just a simple command. I have that part figured out, but all I need now is the code to find the username that I have specified, and if it's on the list, delete it. For example: '!defriend Joe456' --> Look for Joe456 on Friends.TXT --> If found, delete name. That's all I need, nothing fancy, just a simple thing to delete stuff out of text files without having to manually edit the textfiles because that's a pain in the butt.
Jul 29, 2002, 6:44 PM
Veteran (2260 posts)
Jul 29, 2002, 6:44 PM
Post #4 of 7
Views: 5073
grep..
open THISFILEHANDLEISSOLONG, "<friends.txt";
my @list = grep {/^$name$/}, <THISFILEHANDLEISSOLONG>;
close THISFILEHANDLEISSOLONG;
open THISFILEHANDLEISSOLONG, ">friends.txt";
print THISFILEHANDLEISSOLONG join ("\n", @list) . "\n";
close THISFILEHANDLEISSOLONG;
.. you can change the filehandle.
open THISFILEHANDLEISSOLONG, "<friends.txt";
my @list = grep {/^$name$/}, <THISFILEHANDLEISSOLONG>;
close THISFILEHANDLEISSOLONG;
open THISFILEHANDLEISSOLONG, ">friends.txt";
print THISFILEHANDLEISSOLONG join ("\n", @list) . "\n";
close THISFILEHANDLEISSOLONG;
.. you can change the filehandle.
Jul 30, 2002, 6:35 AM
Veteran (19537 posts)
Jul 30, 2002, 6:35 AM
Post #5 of 7
Views: 5020
>>
my @list = grep {/^$name$/}, <THISFILEHANDLEISSOLONG>;
<<
That will never match, as each line will have a newline at the end and you are trying to match ^SomeName$, but you'd want something like this in any case:
my @list = grep { ! /^\Q$name\E\n$/ }, <THISFILEHANDLEISSOLONG>;
...to only match the ones you want, not the one's you don't want. From the first post it looks like the names are in a comma delimited list, not one per line anyway.
If your grep did match (which it won't), using join "\n", @array will then you leave you with blank lines all through your file.
Slurping into an array is also not a good idea if your file is faily lengthy.
Edit: You'll certainly want to use flocking too unless you like fixing corrupt databases.
my @list = grep {/^$name$/}, <THISFILEHANDLEISSOLONG>;
<<
That will never match, as each line will have a newline at the end and you are trying to match ^SomeName$, but you'd want something like this in any case:
my @list = grep { ! /^\Q$name\E\n$/ }, <THISFILEHANDLEISSOLONG>;
...to only match the ones you want, not the one's you don't want. From the first post it looks like the names are in a comma delimited list, not one per line anyway.
If your grep did match (which it won't), using join "\n", @array will then you leave you with blank lines all through your file.
Slurping into an array is also not a good idea if your file is faily lengthy.
Edit: You'll certainly want to use flocking too unless you like fixing corrupt databases.