As already pointed out by Anonymous, the .= effectively appends to the string.

Just to elaborate a little, consider the following (simplified) IN file:

B ... 204248 ... A ... 204249 ... D ... 204250 ... C ... 204251 ... E ... 204252 ... A ... 204253 ... F ... 204254 ... C ... 204255 ... B ... 204256 ...

If you go looking for the IDs A, B, C one after the other in several passes (as in your original approach), the hits will automatically be sorted by ID:

A 204249 # first pas A 204253 B 204248 # second pass B 204256 C 204251 # third pass C 204255

While if you do it in one pass, checking every entry against "if ( exists $scGeneids{$gene_id} )" to see if it's of interest, you'll get the IDs in the order they were encountered in IN, when you print them out immediately

B 204248 A 204249 C 204251 A 204253 C 204255 B 204256

This won't be an issue, of course, in case the entries are already sorted by ID.  Otherwise, it might not be what you want.

You haven't said anything about the desired ordering of the results, nor whether IN is already sorted... so I thought it's worth pointing out anyway that if you wanted output similar to what your original multi-pass approach would have produced (if you had correctly reset the file in between passes), you could collect the results by ID before writing them out. I.e., every time you encounter a hit, you append the line to the respective hash entry.

In other words, after having gone through the file, $scGeneids{A} will hold a long string with all A records, $scGeneids{B} all B records, and so on, so you can nicely print them out in sorted order.

The same result could have been achieved by collecting the records in arrays (i.e. @{ $scGeneids{$gene_id} } ) and then joining them on output, but I thought the string-append variant is a tad easier to understand...


In reply to Re^3: help with loop by Eliya
in thread help with loop by AWallBuilder

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.