How do I delete a random value from an array in Perl?


I'm learning Perl and building an application that gets a random line from a file using this code:

open(my $random_name, "<", "out.txt");
my @array = shuffle(<$random_name>);
chomp @array;
close($random_name) or die "Error when trying to close $random_name: $!";
print shift @array;

But now I want to delete this random name from the file. How I can do this?


If you need to delete a line from a file (its not entirely clear from your question) one of the simplest and most efficient ways is to use Tie::File to manipulate a file as if it were an array. Otherwise perlfaq5 explains how to do it the long way.

By : Schwern

When you say "delete this … from the list" do you mean delete it from the file? If you simply mean remove it from @array then you've already done that by using shift. If you want it removed from the file, and the order doesn't matter, simply write the remaining names in @array back into the file. If the file order does matter, you're going to have to do something slightly more complicated, such as reopen the file, read the items in in order, except for the one you don't want, and then write them all back out again. Either that, or take more notice of the order when you read the file.

By : Tim

  1. shift already deletes a name from the array.

    So does pop (one from the beginning, one from the end) - I would suggest using pop as it may be more efficient and being a random one, you don't care which on you use.

  2. Or do you need to delete it from a file?

    If that's the case, you need to:

    A. get a count of names inside a file (if small, read it all in memory using File::Slurp, if large, either read it line-by-line and count or simply execute wc -l $filename command via backticks.

    B. Generate a random # from 1 to

By : DVK

This video can help you solving your question :)
By: admin