Instead of using explicit while(){} and for(){} loops, I tend to use perl's built-in looping constructs.
# read use Path::Tiny; my @words = ( path($filename)->slurp ) =~ /^.+$/gm;
or
# optional read without Path::Tiny my @words = do { local(@ARGV, $/) = $filename; <> =~ /^.+$/gm }; # or my @words = do { local(@ARGV, $/) = $filename; split /\n/, <> };
and for write
# write use Path::Tiny; use List::Util qw( uniq ); path($filename)->spew(join "\n", uniq(@words), '');
The regex on input was a win for me reading /usr/share/dict/words on my system which has over 123,000 lines in it.
General rule: Don't do things item by item when you can do multiple things at once.
In reply to Re: best way to read and save long list of words
by tybalt89
in thread best way to read and save long list of words
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |