Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Looking for pointers or optimizations.

by aitap (Deacon)
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2016-10-28 20:51 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (387 votes). Check out past polls.