Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Looking for pointers or optimizations.

by aitap (Curate)
on Aug 21, 2012 at 14:10 UTC ( #988751=note: print w/replies, xml ) Need Help??

in reply to Looking for pointers or optimizations.

In addition to the previous comments,

my $words_file = @ARGV;
You assign an array to a scalar. Array is treated in scalar context, and you have only number of arguments in it. Use this instead: my ($words_file) = @ARGV;. In this case an anonymous array gets assigned, and nobody gets harmed.
open (my $fh, ">", $words_file); my @words; while (<>) { push(@words, $_); }
You read STDIN, not the $fh. Use <$fh>, not just <>. Also, in array context the <> operator returns the array with file contents. You can just run my @words = <$fh>;. See readline for more.
my $word = lc($words[rand($words) + 1]);
scalar @words returns count of the words; rand returns any number between (including) 0 and specified number (not including). You should also round this value using int: my $word = lc $words[int rand($words+1)];

Edit: my @words = <$fh>; # not $fh
Sorry if my advice was wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988751]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-05-24 01:33 GMT
Find Nodes?
    Voting Booth?