So you want something like:

use strict; use warnings; my $first_line = <DATA>; chomp $first_line; my $snp_id_count; #my $num_of_strains = 48; print "snp_id\tstrain_id\trs_value\n"; my $skip_strain = '='; while (<DATA>) { chomp; my @fields = split /\t/; my $rs = $fields[2]; my @ids = @fields[4 .. $#fields]; my $strainId; $snp_id_count++; for my $id (@ids) { ++$strainId; next if $id eq '='; print "$snp_id_count\t$strainId\t$rs\n"; } } __DATA__ #chr loc rs observ 129S1 129S4 129X1 A/J 1 3.013441 rs31192577 A/T T T T = 1 3.036178 rs32166183 A/C C C C A 1 3.036265 rs30543887 A/G G G A A 1 3.039187 rs6365082 G/T = = T T

Prints:

snp_id strain_id rs_value 1 1 rs31192577 1 2 rs31192577 1 3 rs31192577 2 1 rs32166183 2 2 rs32166183 2 3 rs32166183 2 4 rs32166183 3 1 rs30543887 3 2 rs30543887 3 3 rs30543887 3 4 rs30543887 4 3 rs6365082 4 4 rs6365082

If you add further columns for more strain Ids it will 'Just Work'™.


Perl reduces RSI - it saves typing

In reply to Re: Looping issue... by GrandFather
in thread Looping issue... by lomSpace

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.