I need to save a growing list of words (>100.000), and occasionally update it. What would be the best way to do it? I need the list in an array to create a regex. At the moment I save it in a plani text file, one word per line. I read it like this:

my @words; my $filename="terms.txt"; if (open my $FH, "<:encoding(UTF-8)", $filename) { while (my $line = <$FH>) { chomp $line; push @words, $line; } close $FH; } my $wordsRX = join "|", map quotemeta, @commonwords;

I write it like this

#I read with the script above my @terms and then add new words to it, +eliminate duplicates, if any, and write it back. my @wordsfiltered = uniq_array(@terms); my $fh = openFileAndWrite($filename); foreach (@wordsfiltered){ print $fh $_ . "\n"; } close $fh;

This runs fine, but I wondering if there are better ways to do it. I am thinking for example at serialisation, incredibly compact. Any drowback?

store(\@wordsfiltered, "terms.array"); my @words= @{ retrieve("terms.array") };

PS: Is there in Perl an easy way to compare the performances (speed) of the two approaces without working directly with timestamps myself?


In reply to best way to read and save long list of words by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.