Problem #1

This code will read your input file 1 line at a time, and filter out just the lines you require, without reading the whole file into memory at once. You would need to redirect the output to a file.

use strict; use warnings; my $flag = 0; while (<DATA>) { if (/^A id/) { print; $flag = 1; } print if $flag and /^M/ } __DATA__ A class 1 1 1 1 1 1 A id 12 12 15 15 16 16 B class 0 0 0 0 0 0 B id 0 0 0 0 0 0 0 C id 0 0 0 0 0 0 0 M X1 1 2 2 2 2 2 M X2 2 1 1 1 1 1 M X3 1 2 2 2 2 2 M X4 2 2 2 1 1 2 M X5 1 1 1 1 1 1 M X6 1 1 1 2 2 1 M X7 1 1 1 1 1 1 M X8 1 1 1 1 1 1 M X9 1 1 1 1 1 1 M X10 1 1 1 1 1 1 M X11 1 2 1 1 1 1 M X12 2 2 2 2 2 2 M X13 2 1 2 2 2 2 M X14 2 1 2 2 2 2 M X15 1 2 1 1 1 1 M X16 1 1 2 2 2 2 M X17 1 2 2 2 2 2

Problem #2

I'm trying to understand your comparison requirements. Could you elaborate?

Update: Here's a guess...

use strict; use warnings; while (<DATA>) { next if /^A id/; my ($x, $c1, $c2, @cols) = (split)[1..7]; print "$x: col 12, 1st: $c1\n"; for my $col (@cols) { if ($c1 == $col) { print " matches\n"; } else { print " does not match\n"; } } print "$x: col 12, 2nd: $c2\n"; for my $col (@cols) { if ($c2 == $col) { print " matches\n"; } else { print " does not match\n"; } } } __DATA__ A id 12 12 15 15 16 16 M X1 1 2 2 2 2 2 M X2 2 1 1 1 1 1 M X3 1 2 2 2 2 2 M X4 2 2 2 1 1 2
Prints...
X1: col 12, 1st: 1 does not match does not match does not match does not match X1: col 12, 2nd: 2 matches matches matches matches X2: col 12, 1st: 2 does not match does not match does not match does not match X2: col 12, 2nd: 1 matches matches matches matches X3: col 12, 1st: 1 does not match does not match does not match does not match X3: col 12, 2nd: 2 matches matches matches matches X4: col 12, 1st: 2 matches does not match does not match matches X4: col 12, 2nd: 2 matches does not match does not match matches

In reply to Re: Column Comparison of a File in Perl by toolic
in thread Column Comparison of a File in Perl by snape

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.