Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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.


Comment on Re: Looking for pointers or optimizations.
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-12-21 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (105 votes), past polls