in reply to Finding a random line
If a word:srand; # seed the random generator open(FILE,"file.txt"); # put the file contents into an array @file = <FILE>; close(FILE); foreach (@file) {chomp} # remove the newlines $line = $file[rand($#file)]; # select a random line
The code is kind of comment-laden, but they're just so you know what's what.srand; # seed the random generator open(FILE,"file.txt"); # put the file contents into one big scalar while(<FILE>) { $file .= <FILE>; } close(FILE); $file =~ s/\r/ /g; # remove carriage returns and replace them with spa +ces $file =~ s/\s+/\s/g; # remove multiple consecutive spaces @file = qw($file); # seperate the file into single words $word = $file[rand($#file)]; # select a random word
|
|---|