in reply to Regex problem

Dear Monks,
I actually want the position in File 1 like suppose 21 of 67890 in this case to be fit in 3 word size window after finding the word FAN and starting the 3 window triplets from
that pattern until it reaches the position 21 and fits this position in 3 window size and prints it out to the output.

I have arrived at this code after some changes but The program is printing the 3 word size only just after the FAN word not talking care of the position in the File 1. Example
in the second case 67890 the position is 21 but the output is printing JAK instead of ALA. That means its printing position just next to FAN always instead of the original
position in File 1.
File 1 looks ilke this:- 12345 14 67890 21 File 2 looks like this:- >gi|12345|ref|OM_2343434|Some text ... ABCDEFANADEFGHIJKLMNOPQRSTVVWXWZ >gi|67890|ref|HM_2338373|Some text ... ABACFHAYJAYAFANJAKALAHUSSGSJISUSSKSOWUWSLSS output should be like this:- 12345 11 FGH 67890 21 ALA Also the File 1 position character should be made bold.
The Code is like this:-
#!/usr/bin/perl use strict; use warnings; my $qfn1 = "File1.txt"; my $qfn2 = "File2.txt"; my %positions; { open(my $fh, '<', $qfn1) or die("Cannot open file \"$qfn1\": $!\n"); while (<$fh>) { my ($key, $pos) = split /\s+/; $positions{$key} = $pos; } } { open(my $fh, '<', $qfn2) or die("Cannot open file \"$qfn2\": $!\n"); for (;;) { defined( my $key = <$fh> ) or last; defined( my $text = <$fh> ) or last; chomp($key); chomp($text); defined( my $pos = $positions{$key} ) or next; $pos = $pos - 1 - 3; $pos >= 0 or next; my ($str) = $text =~ /^.{0,$pos}FAN(.{3})/ or next; print("$key: $str\n"); } }

20081224 Janitored by Corion: Restored content

Replies are listed 'Best First'.
Re: Extracting Locations in 3 window size
by ig (Vicar) on Dec 20, 2008 at 17:18 UTC

    The following produces the output you describe, except for the bolding. For hilighting you might have a look at Term::ANSIColor.

    #!/usr/bin/perl use strict; use warnings; my $qfn1 = "File1.txt"; my $qfn2 = "File2.txt"; my %positions; { open(my $fh, '<', $qfn1) or die("Cannot open file \"$qfn1\": $!\n"); while (<$fh>) { my ($key, $pos) = split /\s+/; $positions{$key} = $pos; } } { open(my $fh, '<', $qfn2) or die("Cannot open file \"$qfn2\": $!\n"); for (;;) { defined( my $key = <$fh> ) or last; defined( my $text = <$fh> ) or last; chomp($key); chomp($text); $key = (split(/\|/,$key,3))[1]; defined( my $pos = $positions{$key} ) or next; my $index = rindex($text, "FAN", $pos); next if ( $index < 0 ); $index += 3 while ( ($index + 3) < $pos); print "$key $pos " . substr($text, $index, 3) . "\n"; } }