What you're wanting to do is report generation, and this is old school COBOL type processing at its best. Fortunately, it's a common idiom that can be put into any procedural language, including (especially?) Perl.

You need two variables to keep up with the 1st column (the 1, 2, ...) and the 2nd column (the 300, 320, 312, ...).

$old_col_0 = ''; $old_col_1 = ''; while (<FILE>) { #containing the above information chomp; my @temp_array = split(/\t/,$_); for (1 .. 2) { #arbitary number based on outside factor my $out_line = ''; if ($temp_array[0] ne $old_col_0) { $out_line = $temp_array[0]; # Space after data $out_line .= " "; $old_col_0 = $temp_array[0]; } else { # 1 space where data would have been, plus 1 more for # column spacing $out_line = " "; } if ($temp_array[1] ne $old_col_1) { # Make sure all subsequent uses of out_line are # append, not assignment $out_line .= $temp_array[1]; $out_line .= " "; $old_col_1 = $temp_array[1]; } else { # Four spaces, 3 for data, 1 for column spacing $out_line .= " "; } # A handy join to get the rest of the data # We don't want the 0th or 1st elements, they're # handled specially. $out_line .= join(" ", @temp_array[2..5]); print $out_line;

In reply to Re: Loop, but print once. by Sinistral
in thread Loop, but print once. by BioNrd

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.