For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > need help for Getopt::Long;









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author need help for Getopt::Long;
Shiping Wang

2005-03-25, 3:56 pm


Hi, I have problem to match array defined in Getopt::Long and transfer to
new file:
If I do:
try.pl --InputData sample.txt --Trait=_BMI --covars=age, _DBP, _SBP
--Race=Others
with this file:
GFAMID GDADID GMOMID ID SEX
HYT3 _SBP _DBP _BMI RACE AGE _HTMED antiht

How can I get it to new file like this:

A HYT3
C _SBP
C _DBP
T _BMI
S RACE
C AGE
S _HTMED
S antiht

Thanks!!!

SW

########################################
#######################################

#!/usr/local/bin/perl
# try.pl
use warnings;
use strict;
use Getopt::Long;
my $inputData =' ';
my $trait= ' ';
my $race;
my @covars;
my $result = GetOptions("InputData=s" => \$inputData,
"Trait=s" => \$trait,
"Race=s" => \$race,
"Covars=s" => \@covars
);
my @covarlist = split(/[\s|,]/, @covars);
if(!$inputData){
exit();
}
#die "you must specify all parameters\n\t--Trait --Race --Covars\n"
unless(define $trait || define $race || define @covars);
my $oriname = (split/\./, $inputData)[0];
my $outname;
if ($race){
$outname = "$oriname\_$race";
} else{
$outname = "$oriname";
}


open (outDat, ">$outname.dat") or die $!;
open (inPed, "$inputData") or die $!;

my $header = <inPed>;
chomp $header;
my @headline = split/\t/, $header;

my @ped_n_trait = @headline[0, 3, 1, 2, 4..12];
my %vartype;
foreach my $i (@ped_n_trait[5..12]){
if ($i eq 'HYT3'){
$vartype{$i} = 'A';
}elsif ($i eq "$trait"){
$vartype{$i} = 'T';
}else{
$vartype{$i} = 'S';
for(@covarlist){
if($_ eq "$i"){
$vartype{$_} = 'C';
}
}

}
}
for (@ped_n_trait[5..12]){
print outDat "$vartype{$_}\t$_\n";
}

close outDat;

close inPed;



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com