|
This tutorial will teach you how to open a text file in Perl, and return a random
line out of the file.
# Open the text file (we'll call it "file.txt").
open (DATA, "./file.txt");
# Get all the data from the file into an array.
my @data = <DATA>;
# @data now is an array with each line of the file in it.
# Close the file because we're done using it.
close (DATA);
# Remove ending line breaks (the "enter" at the
# end of each line) from @data.
chomp @data;
# Pick a variable to put a random line into.
my $randline = $data [ int(rand(scalar(@data))) ];
# $randline is now a random line from the data.
|
And that's all there is to it!
|