in reply to Pick random item from list of unknown length?

I tried the following using an annonymous array and dereference to get a single expression.

Unfortunatly, since it requres TWO splits (one to generate the list of item in a line and one to figure out how many items are in that line), I don't think it is any more memory efficient.

In fact, I suspect it is LESS memory efficient since the annonymous arrays (mine uses two whereas the case the OP is trying to get rid o uses on a single array) take up memory.

But, as others pointed out, the OP wasn't clear on whether the issue was saving space or making the code takes less lines. So this appraoch ONLY saves lines...at the expense, IMHO, of readability and memory.

#!/usr/bin/perl use strict; use warnings; my @lines = ( "apple", "apple,banana,cherry", "banana,cherry", "apple,cherry", "banana", "cherry", ); foreach my $line (@lines){ my $item = (split(/\,/,$line))[int(rand(scalar(@{[split(/\,/,$line)]})))]; print "$item\n"; } exit(0);
ack Albuquerque, NM