Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with loops

Status
Not open for further replies.

Capgemini

MIS
Oct 1, 2001
2
GB
I'm learning perl from a book and I'm a bit at lost.
here is my problem:
as you may have guessed, I'm trying to read from this text file into an array and extract the "exon" value and the corresponding name from the file and print it to another file. So far I was able to match value pair by doing the split operation and I was able to setup a key{}. My problem is that only one value pair is produced when I execute the code. I have tried several ways of doing a loop but they either fail or produce the same result. I have tried pattern matching and using the grep command, but nothing came out of. maybe I'm doing this all wrong :(. Any help would be appreciated.





Here is my data:

Mike 3093 3132 R promoter
John 3500 3912 R disease_locus
Jake 2727 2776 R transposon
sunn 2519 2536 F disease_locu
rich 1969 2310 R exon
Jenn 3788 4369 R exon
Joan 3865 4131 F exon


Here is my code :

open FILE, "C:/sent_data.txt";
while (<FILE>) {
@x= split;
@name{$x[4]} = $x[0];}
$geneToFind = shift @ARGV; {
print &quot;Gene: $geneToFind\n&quot;;
print &quot;Name: &quot;, $name{$geneToFind}, &quot;\n&quot;;
}

 
you need a

chomp();

before the split()

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
here you go, this complete your script. you have the right idea.
if ($ARGV[0] eq undef )
{
print &quot;You must pass the gene as a command-line parameter!\n&quot;;
}
else
{
$geneToFind=$ARGV[0];

#print &quot;Gene to find: $geneToFind\n&quot;;

while ( <hFile> )
{
chomp;
@x=split( /\s+/, $_ );
if ( $x[4] eq $geneToFind )
{
print &quot;Field 1: $x[0]\tField2: $x[1]\tField 3: $x[2]\tField4: $x[3]\n&quot;;
}
}
}
close ( hFile );
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top