perl search algorithm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    perl search algorithm

    Hi, there are two text files as below:

    test1.txt

    Code:
    53015910        53018079
    53028477        53039398
    53057449        53060200
    53062918        53067636
    53068725        53070903
    53073104        53082405
    53157340        53159848
    53162114        53164278
    53170778        53177010
    53186077        53190880
    test2.txt is a tab delimited file and I want to check whether col 13 and col 35 of this values (each line) lies between test1.txt columns. I am trying to search as below:

    1. I am adding the contents of test1 into a hashname
    2. For each line in the second file, I am checking whether col 13 and col 35 lies between (key and value) of hashname
    3. I am trying to interrupt once if the status is set to 1 so that I can stop avoiding the whole of hashname.

    Code:
    #!/usr/bin/perl
    
    $st_status;
    $end_status;
    
    #adding contents into a hash
    
    $hashfile = 'test1.txt';
    open (LIST1, $hashfile) || die "File not found\n";
         while (<LIST1>) {
                    chomp $_;
              @v = split(/\t/, $_);
              $hashname{$v[1]} = $v[2];
         }
    close(LIST1);
    
    
    #Search in the second file
    
      while(<>) {
    
            $st_status = 0;
            $end_status = 0;
      
            chomp $_;
            @cols=split(/\t/,$_);
                    
            while (($key, $value) = each(%hashname)){
                     if(($cols[12] >= $key) && ($cols[12] <= $value)) {
                            $st_status = 1;                        
                            last;
                    } 
                   
            }
    
            while (($key, $value) = each(%hashname)){
                     if((cols[34] >= $key) && (cols[34] <= $value)) {
                            $end_status = 1;                        
                            last;
                    }
                    
            }
    
            if (($st_status == 1 ) && ($end_status == 1)) {               
                    print "$_\n";                
            } else { next; }
    
    }
    
    close FILE;
    Is there any better way to do this? This is taking quite a long time. Thanks.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Can you please provide a sample of the second text file? Without it we would be guessing that the solution works.

    Regards,

    Jeff

    Comment

    • lilly07
      New Member
      • Jul 2008
      • 89

      #3
      Hi Jeff,

      my sample test2.txt contains the following in the 13th and 35th column.

      Code:
      57583225        57580491
      57583262        57580492
      58049166        58012648
      57583215        57580442
      59416357        59435981
      56506459        56164151
      63518481        63509149
      58053224        58138139
      59675405        60296224
      59675408        60296279
      Hence for every line in test2.txt, I have to check whether "57583225" lies between any on the test1.txt record (e.g 53015910 53018079.. ) as well as "57580491" lies in between any of the record on test1.txt.
      Please let me know if u need any more info. Thanks.

      Comment

      Working...