Here is a somewhat long-winded solution which I hope is self-documenting. Assumes input file is something like what you can see in the __DATA__ section - you will need to replace <DATA> with your opened filehandle:
use strict; use warnings; use Data::Dumper; my (@results,%hoh,$current); while (my $line = <DATA>) { chomp $line; next unless $line; my ($col_name,$rest) = split(/\s/,$line,2); if ($col_name =~ m/^partnm_/) { push(@results,$col_name); $hoh{$col_name}{'partnm'} = $rest; $current = $col_name; next; } next unless $current; $hoh{$current}{$col_name} = $rest; } print Dumper(\%hoh); # to see what we've got so far my @cols = qw( partnm prlot prwafer prbin sspec fab ); my $hline = join(',',@cols); print "$hline\n"; for my $id (@results) { my $hash = $hoh{$id}; my @elems; for my $col (@cols) { my $val = $hash->{$col} || 'empty'; push(@elems,$val); } my $line = join(',',@elems); print "$line\n"; } __DATA__ partnm_1 101 prlot prlot_1 prwafer prwafer_1 prbin prbin_1 sspec sspec_1 fab fab_1 partnm_2 102 sspec sspec_2 fab fab_2 partnm_3 103
Output:
partnm,prlot,prwafer,prbin,sspec,fab 101,prlot_1,prwafer_1,prbin_1,sspec_1,fab_1 102,empty,empty,empty,sspec_2,fab_2 103,empty,empty,empty,empty,empty

In reply to Re: Print Match name for certain rows by tangent
in thread Print Match name for certain rows by penny1

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.