By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

perl search algorithm

P: 89
Hi, there are two text files as below:

test1.txt

Expand|Select|Wrap|Line Numbers
  1. 53015910        53018079
  2. 53028477        53039398
  3. 53057449        53060200
  4. 53062918        53067636
  5. 53068725        53070903
  6. 53073104        53082405
  7. 53157340        53159848
  8. 53162114        53164278
  9. 53170778        53177010
  10. 53186077        53190880
  11.  
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $st_status;
  4. $end_status;
  5.  
  6. #adding contents into a hash
  7.  
  8. $hashfile = 'test1.txt';
  9. open (LIST1, $hashfile) || die "File not found\n";
  10.      while (<LIST1>) {
  11.                 chomp $_;
  12.           @v = split(/\t/, $_);
  13.           $hashname{$v[1]} = $v[2];
  14.      }
  15. close(LIST1);
  16.  
  17.  
  18. #Search in the second file
  19.  
  20.   while(<>) {
  21.  
  22.         $st_status = 0;
  23.         $end_status = 0;
  24.  
  25.         chomp $_;
  26.         @cols=split(/\t/,$_);
  27.  
  28.         while (($key, $value) = each(%hashname)){
  29.                  if(($cols[12] >= $key) && ($cols[12] <= $value)) {
  30.                         $st_status = 1;                        
  31.                         last;
  32.                 } 
  33.  
  34.         }
  35.  
  36.         while (($key, $value) = each(%hashname)){
  37.                  if((cols[34] >= $key) && (cols[34] <= $value)) {
  38.                         $end_status = 1;                        
  39.                         last;
  40.                 }
  41.  
  42.         }
  43.  
  44.         if (($st_status == 1 ) && ($end_status == 1)) {               
  45.                 print "$_\n";                
  46.         } else { next; }
  47.  
  48. }
  49.  
  50. close FILE;
  51.  
Is there any better way to do this? This is taking quite a long time. Thanks.
Dec 8 '09 #1
Share this Question
Share on Google+
2 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Can you please provide a sample of the second text file? Without it we would be guessing that the solution works.

Regards,

Jeff
Dec 8 '09 #2

P: 89
Hi Jeff,

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

Expand|Select|Wrap|Line Numbers
  1. 57583225        57580491
  2. 57583262        57580492
  3. 58049166        58012648
  4. 57583215        57580442
  5. 59416357        59435981
  6. 56506459        56164151
  7. 63518481        63509149
  8. 58053224        58138139
  9. 59675405        60296224
  10. 59675408        60296279
  11.  
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.
Dec 9 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.