Hi, I have a list of files that contains multiple lines. I get those lines into an array, then I would like to push those into a hash with map for later quick search.
for(glob("*.gz")){ my @o = `zcat $_ | sed 's/[<> ]//g'`;chomp @o;push @l,@o; } my %h = map { $_, 1 } @l;
I am trying to remove "my %h = map { $_, 1 } @l;" and "push @l,@o" to use less memory and maybe speed up a bit the process. Any good idea?

====== Update

zcat file1.gz will return :
- line1 xxxxx - line2 yyyyy - line3 zzzzz
the array @l is containing for each turn of the loop :
- file1_line1 xxxxx - file1_line2 yyyyy - file1_line3 zzzzz - filen_line1 xxxxxxx - filen_line2 yyyyyyy - filen_line3 zzzzzzz
then the hash %h is containing
xxxxx -> 1 yyyyy -> 1 zzzzz -> 1 xxxxxxx -> 1 yyyyyyy -> 1 zzzzzzz -> 1
The amount of keys are counted in millions. So, using a hash is much better than using a grep in array to find if a key exist or not later on. Every little bits count, so even if i didnt profiled the code I did both tries with hash and grep and to accomplish the whole treatment with a grep it takes about 15min and with a hash it takes about 1min.

In reply to add/replace map result into existing hash by fredo2906

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.