Home: Perl Programming Help: Regular Expressions:
Search and extraction help



perlfree
Novice

Mar 3, 2010, 3:56 PM


Views: 8967
Search and extraction help

Hi,

I need help to make a perl program work. The program accepts an input (reaction) and then search for the input in a file and then displays the reaction on the same line.

Input file - file.txt (A large file with no header and in the following format):
A1_HTTT24 : GLUC_ext = GLUC .
C2_GLH3 : GLUC + ATP = GLUC6P + ADP .
B3_PGAI1 : GLUC6P = FRUC6P .

Search example: I want to search with, for example, A1_HTTT24, and then get an output as: GLUC_ext = GLUC


Here's what I have done so far:

$database = 'file.txt';
open(Dbase,"<$database") or die "can't open $database $!";
while (my $line = <Dbase>){
chomp $line;

@all = split /\s+/, $line;
($reac,$eqn) = split(':',$all);
chomp $reac;
chomp $eqn;
$reac =~ /([^\s]+)/;
$reac = $1;
$eqn =~ /(^\s+)(.+)(\s\.)/;
$eqn = $2;

#@reac = $reac;
@eqn = $eqn;
#$reac =~ s/\s//g;

}
close(Dbase);

open (DATA,"+>data.txt") or die "Can't open data";
do {
print "reaction name for searching: \n";
$input = <>;
chomp $input;

while (@all){

foreach $r (@reac){
$flag = 0;

foreach $e (@eqn){

if ($r eq $input){
$flag = 1;
last;
print DATA "$r,$eqn{$r}\n";
}else{

print "$input not found!\n";

}
}
}


}

close(DATA);
}until ($input =~ /^\s*$/);
exit(0);


Thanks,

Jamie

In Reply To



(This post was edited by perlfree on Mar 3, 2010, 5:04 PM)


rovf
Veteran

Mar 5, 2010, 4:48 AM


Views: 8937
Re: [perlfree] Search and extraction help


Quote

Code
$database = 'file.txt'; 
open(Dbase,"<$database") or die "can't open $database $!";
while (my $line = <Dbase>){
chomp $line;

@all = split /\s+/, $line;
($reac,$eqn) = split(':',$all);



Rule number one: Always use 'strict' and 'warnings'. It would have saved you a lot of work. For example, you $all is undef in your code.

Now for your basic problem: In your input file, the key and the reaction always seem to be separated by /\s*:\s*/, so it doesn't make sense to split first on whitespaces, and then again on a colon.


FishMonger
Veteran / Moderator

Mar 5, 2010, 5:43 AM


Views: 8935
Re: [rovf] Search and extraction help

rovf,

This is a cross posted question.
http://forums.devshed.com/perl-programming-6/data-search-and-extraction-681918.html


Nila
User


Mar 6, 2010, 3:04 AM


Views: 8918
Re: [perlfree] Search and extraction help

I got your requirement.I think it can be solved easily.I have done the changes and I tested the code in my machine.It is working.You check it.


Code
use strict; 
use warnings;
use Data::Dumper;

my $database = 'file.txt';
my (%hash,$input);
open(Dbase,"<$database") or die "can't open $database $!";
while (my $line = <Dbase>){
chomp $line;
my ($key,$val)=split('\s*:',$line);
$hash{$key}=$val;
}
close Dbase;
#print Dumper \%hash;
open (DATA,"+>data.txt") or die "Can't open data";
do {
print "Enter reaction name for searching:";
$input=<>;
chomp($input);
if(exists $hash{$input}) {
print DATA "$input,$hash{$input}\n";
}
else {
print "Given Key isn't found in the file\n";
}
}until($input=~/^\s*$/);
close(DATA);



perlfree
Novice

Mar 6, 2010, 2:19 PM


Views: 8912
Re: [rovf] Search and extraction help

Thanks for your suggestions, rovf.


perlfree
Novice

Mar 6, 2010, 2:22 PM


Views: 8911
Re: [perlfree] Search and extraction help

Hi Nila,

Many thanks for your code. However, for some reason there was no output in the file. I wonder if this line:

print DATA "$input,$hash{$input}\n";

should be:

print DATA "$input,$hash{$key}\n";

Regards,