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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop in hash 1

Status
Not open for further replies.

noyk

MIS
Feb 20, 2009
23
MY
Hi, I am encountering some problem with looping here. How do get this line "if ($line =~ m ($gene)) {" to match all $gene in the hash?

open (LIST1, "tab_delimited.txt");
while (<LIST1>) {
($id, $product, $ec) = split(/\t/, $_);
$hash{$id} = ($product, $ec);
}
close(LIST1);
open (LIST2, "tab.txt");
while (<LIST2>) {
my($line) = $_;
if ($line =~ m ($id)) {
print $line;
print "\n /product=\"$product\"";
if ($ec) {print "\n /EC_number=\"$ec\""};
print "\n"
}
else {
print $line;
}
}
close(LIST2);

the goal is to insert a few rows of info into the file tab.
this is the content for file tab

CDS /label=2494_g
/id="LAN0001"
CDS /label=2494_g
/id="LAN0002"
CDS /label=2495_g
/id="LAN0003"

and the tab_delimited file

LAN0001 ABC
LAN0002 DEF 123
LAN0003 GHI

the expected output

CDS /label=2494_g
/id="LAN0001"
/product="ABC"
CDS /label=2494_g
/id="LAN0002"
/product="DEF"
/EC_number="123"
CDS /label=2495_g
/id="LAN0003"
/product="GHI"

thanks!
 
You can't with that construct (and please use [ignore]
Code:
(your code here)
[/ignore] tags when listing code and data).
First you should write: [tt]$hash{$id}=[$product,$ec];[/tt]
Code:
while(<LIST2>) {
  if(/id=/){
    print;
    chomp;
    (undef,$id)=split/=/;
    $id=eval$id;
    if(exists$hash{$id}){
      print"                     /product=\"$hash{$id}[0]\"\n";
      print"                     /EC_number=\"$hash{$id}[1]\""if$hash{$id}[1];
    }
  }else{
    print $line;
  }
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
thanks..
but it doesn't work though.. i'm guessing there are some problems at these lines?

Code:
open (LIST1, "tab_delimited.txt");
while (<LIST1>) {
($id, $product, $ec) = split(/\t/, $_);
$hash{$id} = [$product, $ec];
              }
close(LIST1);
open (LIST2, "tab.txt");

while(<LIST2>) {
  if(/id=/){
    print;
    chomp;
    (undef,$id)=split/=/;
[B]    $id=eval$id;
    if(exists$hash{$id}){[/B]
      print"                     /product=\"$hash{$id}[0]\"\n";
      print"                     /EC_number=\"$hash{$id}[1]\""if$hash{$id}[1];
    }
  }else{
    print;
  }
}
 
Works for me with two small errors:
-a [tt]\n[/tt] is missing in the line for ED_number
-[tt]print $line;[/tt] should be [tt]print;[/tt] (as you noticed).
Don't know if your data is really tab delimited; anyway you should write the line [tt]($id,$product,$ec)=split(/\t/,$_);[/tt] as [tt]($id,$product,$ec)=split;[/tt]: it will work also with (multiple) spaces as delimiters.
Make also sure that your files have a line terminator also in the last line.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top