473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

perl search algorithm

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
2 1620
numberwhun
3,509 Expert Mod 2GB
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
lilly07
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

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

Similar topics

1
by: Cameron Laird | last post by:
In article <iL3jc.232$Yc.2330@news4.e.nsc.no>, Leif B. Kristensen <junkmail@solumslekt.org> wrote:
42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
77
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for...
3
by: lonelyplanet999 | last post by:
Hi, I'm a newbie to perl and is now studying about perl programming, I read some perl programming tutorials online (enter 'Perl tutorial' at google.com) and also find some sample perl scripts...
2
by: Fredrik Ramsberg | last post by:
When searching for a specific sequence of charcters, like ($text =~ /monkeybreath/), how efficient is Perl compared to other algorithms specifically developed for this purpose? Does Perl recognise...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
6
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.