in reply to Re: File Manipulation
in thread File Manipulation

So you want to remove a random element, here's the code that works for me.
$random_file = "test.txt"; open (FILE, $random_file); @LINES=<FILE>; close(FILE); srand; # Choose a random line #scalar(@LINES) is the number of elements in the array $OTP = "$LINES[(int rand(scalar(@LINES)))]"; $PWFILE = $OTP; open (FILENEW, ">$random_file"); foreach $LINES (@LINES) { if ($PWFILE eq $LINES){ $LINES = "" } else { print FILENEW $LINES; } } close (FILENEW); # If you want to remove the newline, I don't know much # about html stuff...it may cause problems? #chomp( $OTP ); $PPP = $OTP;
I tested it with a file that looks like this:
test test1 test2 test3 test4
It removed a random element from the file, and had it in $PWFILE. I don't know about the rest of the file, but you could probably remove $PPP and $PWFILE and just use $OTP.

I also did not fix the style, I would avoid using all capital var. names except for filehandles.

Hope this helps!