However, if you really prefer to use a flat file you could try somehing like the following. It selects a random line in the file and does an in-place edit so that the line is effectively removed.
However, if the selection doesn't have to be random it would be more efficient to read a record from the end and then truncate the file as shown below.#!/usr/bin/perl -w use strict; my $passwords = 'passwords'; # It would be better to cache the $count value in a separate file my $count = `wc -l $passwords`; # Bail out if wc failed exit if $?; # Get the line count $count = (split ' ', $count)[0]; # Bail out if the file is empty die "No passwords in $passwords\n" unless $count; my $key = 1 + int rand $count; my $password; # Localised in-place edit { local $^I = ''; local @ARGV = $passwords; while (<>) { if ($. == $key) { chomp; $password = $_; } else { print; } } } print $password, "\n"; __END__
--
John.
In reply to Re: popping or shifting a line off a file
by jmcnamara
in thread popping or shifting a line off a file
by nmerriweather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |