in reply to Re: File Manipulation
in thread File Manipulation
I tested it with a file that looks like this:$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;
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.test test1 test2 test3 test4
|
|---|