in reply to 2 newby questions
In addition to the good replies above, this:
chomp(@words = <STDIN>); foreach $word (@words){ $count{$word}++ } # is better done like this: while( my $word = <STDIN> ){ chomp $word; $count{$word}++; }
As you can see, the second idiom doesn't require the @words array at all, since it reads and processes the data one line at a time. That doesn't matter much for small amounts of data, but for larger amounts the first method could run into memory constraints. It's best to use the second method by default unless you must use the first for some reason.
Aaron B.
Available for small or large Perl jobs; see my home node.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: 2 newby questions
by Socrates440 (Acolyte) on Jun 14, 2012 at 23:22 UTC | |
by aaron_baugher (Curate) on Jun 15, 2012 at 04:38 UTC | |
by hbm (Hermit) on Jun 15, 2012 at 11:57 UTC | |
by Anonymous Monk on Jun 15, 2012 at 13:09 UTC |