Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: searching an array member in file header and print a column

by InfiniteSilence (Curate)
on May 27, 2014 at 15:05 UTC ( #1087551=note: print w/replies, xml ) Need Help??


in reply to searching an array member in file header and print a column

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

Replies are listed 'Best First'.
Re^2: searching an array member in file header and print a column
by shalgham (Initiate) on May 27, 2014 at 15:47 UTC
    Thanks a lot, You are right I am both new to perl and programming.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1087551]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-01-28 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (73 votes). Check out past polls.

    Notices?