If you can ensure that each line is of a constant length by space padding to length of the longest pice of data, you could use seek to move directly to the line you pick randomly.
A second way would be to pre-read the file line by line and record the start position of each line in an array. You could do this using something like
my @positions = (0); open FILE, '<', 'file' or die $!; while(<FILE>) { push @positions, tell FILE; }
You then select your line by picking a random position from the array, seek to it and read your data.
my $randData = do{ seek FILE, $positions[rand @positions], 0; <FILE>; +}
Note: Untested code. Read the docs and check the syntax etc.
Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.
In reply to Re: reading a random line from a file
by BrowserUk
in thread reading a random line from a file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |