#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Data search and extraction


    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:


    PHP 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);
     
    chomp $reac
     
    chomp $eqn
     
    $reac =~ /([^\s]+)/;
     
    $reac = $1;
     
    $eqn =~ /(^\s+)(.+)(\s\.)/;
     
    $eqn = $2;
     } 
     
    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
    Last edited by perlfree; March 4th, 2010 at 08:01 AM. Reason: corrected code layout
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,040
    Rep Power
    1228
    Please fix your code formatting, it's currently really bad.

    You're using the wrong pattern in the split. split on /\s+:\s+/

    e.g.,
    Code:
    while (my $line = <Dbase>) {
        chomp $line;
        my ($key, $value) = split(/\s+:\s+/, $line, 2);
    
        # assuming $line = 'A1_HTTT24 : GLUC_ext = GLUC'
        # $key will eq 'A1_HTTT24'
        # $value will eq 'GLUC_ext = GLUC';
    ....
    ....
    }
    Last edited by FishMonger; March 4th, 2010 at 07:42 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0
    Apologies for the bad code format and thanks for your reply.

    I've implemented your suggested line, but I'm still having problems. Here is my latset code:

    PHP Code:
    $database 'reaction_database.txt';
      
    open(Dbase,"<$database") or die "can't open $database $!";

      while (
    my $line = <Dbase>){     
      
    chomp $line;
      
    my ($key$value) = split(/\s+:\s+/, $line2);
      
    chomp $key;
      
    chomp $value;
      
    $key =~ /([^\s]+)/;
      
    $key = $1$value =~ /(^\s+)(.+)(\s\.)/;
      
    $value = $2;
      } 
      
    close(Dbase);

      
    open (DATA,"+>data.txt") or die "Can't open data $!";
     
    # do { 

      
    print "reaction name for searching: \n";
      
    $input = <STDIN>;
      
    chomp $input
      while (
    defined<$line>){ 
           foreach 
    $k (@value){ 
                    if (
    $key eq $input){ 
                    print 
    DATA "$key,$value{$key}\n"
                       }else{ 
                          print 
    "$input not found!\n";     
                     } 
          }  
       
      }
      
    close(DATA); 

     
    # }until ($input =~ /^\s*$/);
      
    exit; 
    Originally Posted by FishMonger
    Please fix your code formatting, it's currently really bad.

    You're using the wrong pattern in the split. split on /\s+:\s+/

    e.g.,
    Code:
    while (my $line = <Dbase>) {
        chomp $line;
        my ($key, $value) = split(/\s+:\s+/, $line, 2);
    
        # assuming $line = 'A1_HTTT24 : GLUC_ext = GLUC'
        # $key will eq 'A1_HTTT24'
        # $value will eq 'GLUC_ext = GLUC';
    ....
    ....
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,040
    Rep Power
    1228
    I guess the message in my example test code was a little too subtle.

    You need to build a hash of your database data and once that is done, searching the data becomes a very simple 1 line that does a hash lookup.

    The more I look at your code, it appears to me that this is your homework assignment.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0
    Thanks for your suggestions. This code is actually my personal project as I'm only trying to improve my perl skills - glad that I have no submission deadline on this.

    Originally Posted by FishMonger
    I guess the message in my example test code was a little too subtle.

    You need to build a hash of your database data and once that is done, searching the data becomes a very simple 1 line that does a hash lookup.

    The more I look at your code, it appears to me that this is your homework assignment.

IMN logo majestic logo threadwatch logo seochat tools logo