Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by 2teez (Vicar)
on May 27, 2014 at 19:54 UTC ( [id://1087573]=note: print w/replies, xml ) Need Help??


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

Hi shalgham,
Without removing anything from previous great post, I think ( that is if I get your question right ) what you need do is get the index of the column you wanted and print it out. Since all the values for the column you wanted follow the same.
Something like this, using a modified data given by InfiniteSilence

#!/usr/bin/perl -l use warnings; use strict; # get the header into an array my @header = split /\s+/, <DATA>; # conjure a regex using the name of columns you wanted # which is the array you have my $to_find = join( '|' => qw(nat pls kac) ); my $reg_to_use = qr/$to_find/; # get the index of the header you need my @index = grep { $header[$_] =~ /$reg_to_use/ } 0 .. $#header; print join( "\t" => @header[@index] ); while (<DATA>) { print join( "\t" => ( split /\s+/, $_ )[@index] ); } __DATA__ nat pls fof tri pls fof tri kac 0.1 0.1 0.23 0.1 0.1 0.23 0.1 0.31 2.3 1.8 3.2 4.4 1.8 3.2 4.4 3.21 5.5 3.2 8.6. 7.9 3.2 8.6. 7.9 2.89
Output:
nat pls pls kac 0.1 0.1 0.1 0.31 2.3 1.8 1.8 3.21 5.5 3.2 3.2 2.89
Note: I don't know how your real dataset looks like, and the above 'might' not be the best in your situation but I can only hope this gives a head up.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found