Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: best way to read and save long list of words

by tybalt89 (Monsignor)
on Apr 19, 2020 at 01:24 UTC ( [id://11115763]=note: print w/replies, xml ) Need Help??


in reply to best way to read and save long list of words

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found