CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



perlfree
Novice

Mar 3, 2010, 3:56 PM

Post #1 of 6 (5032 views)
Search and extraction help Can't Post

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

Post #2 of 6 (5002 views)
Re: [perlfree] Search and extraction help [In reply to] Can't Post


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

Post #3 of 6 (5000 views)
Re: [rovf] Search and extraction help [In reply to] Can't Post

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

Post #4 of 6 (4983 views)
Re: [perlfree] Search and extraction help [In reply to] Can't Post

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

Post #5 of 6 (4977 views)
Re: [rovf] Search and extraction help [In reply to] Can't Post

Thanks for your suggestions, rovf.


perlfree
Novice

Mar 6, 2010, 2:22 PM

Post #6 of 6 (4976 views)
Re: [perlfree] Search and extraction help [In reply to] Can't Post

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,

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives