The normal, correct answer to a question like this in Perlmonks is for you to go and spend some additional time learning Perl. However, I think your problem has less to do with Perl and more to do with some type of lack of understanding of data structures and logical flow so I'll try to address these with an example that closely matches your problem:

#!/usr/bin/perl -w use strict; use Data::Dumper; my @cols = qw(nat pls kac); my @bigStructure = (); my $realHeaders; my $lineCntr = 0; sub procHeader { my $hdrline = shift; my @instArray = (); my %hash = map {$_=>1} @cols; #thanks kcott my @contents = split /\s+/,$hdrline; for(@contents) { if($hash{$_}) { push @instArray,$_; } else { push @instArray, qw|skip|; } } return \@instArray; } while(<DATA>) { if(!m/^\d/){$realHeaders = procHeader($_)}; my @row = split /\s+/; for (0..$#{$realHeaders}) { if($realHeaders->[$_] ne 'skip') { push @{$bigStructure[$lineCntr]}, $row[$_]; } } $lineCntr++; } print Dumper \@bigStructure; 1; __DATA__ nat pls fof tri 0.1 0.1 0.23 0.1 2.3 1.8 3.2 4.4 5.5 3.2 8.6. 7.9

Yields, (Edit: - moved $lineCntr to the end -- forgot arrays in Perl are zero-based)

$VAR1 = [ [ 'nat', 'pls' ], [ '0.1', '0.1' ], [ '2.3', '1.8' ], [ '5.5', '3.2' ] ];

This entire program could probably be reduced to a single or a few lines. It works by creating an intermediate structure, an array of so-called 'real' headers, with the 'skip' string where you do not want that value to be propagated. Then iterating over the row values is trivial.

Looking at your pseudocode tells me that, like a lot of people learning Perl, you start thinking about a programming problem by first looking at the would-be operations. However, I recommend that you revise that thinking to start by considering the data structures instead. When I did this I recognized that I really should be using arrays rather than hashes for most of my program because I want to keep everything in the same order.

Lastly, your example would actually only print two columns since nat and pls are both valid columns but fof and tri are not.

Celebrate Intellectual Diversity


In reply to Re: searching an array member in file header and print a column by InfiniteSilence
in thread searching an array member in file header and print a column by shalgham

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.