in reply to Working on Deleting Sections of Text File Delimited by Newlines

I agree that you should either use the CGI module or not use it but not include it and then ignore it. I was going to say something along the lines of you should name all of your checkboxes the same and then just cycle through the values, but when I considered the code snippet below, that didn't work as well.... but often you should do that.

Now I suspect that you're not asking about how to generate this form, but what to do with it after submission... as in how can you remove the actual posts from your file. You said that your file is separated by newlines. The easiet way - that I can see - to modify your file is to read it all in, change it and then write it out again. (just make sure that it won't ever be too huge). So... something like:

$/ = ... # whatever my @posts = <FH>; # read all posts in at once and assign # each to an array element. my @keep_posts; for(my $index = 0; $index < @posts; $index++) { unless(defined($cgi->param("box$index")) { push @keep_posts, $posts[$index]; } } # open file... print all the kept ones out. print NEW_FILE join("\n\n\n", @keep_posts);
should do the trick nicely.

If this wasn't what you wanted, please ask again.

update After actually reading cLive ;-)'s post I feel silly for saying much the same thing. His way has the definate advantage of not needing to read the entire thing into memory, just handling it post by post. That's probably the better approach.

  • Comment on Re: Working on Deleting Sections of Text File Delimited by Newlines
  • Download Code