3

I tried to print the matched string at the postion of data,how can i do it?

$at ="at";
@ar = <DATA>;
@xxv = map(m/$at/g, @ar);
print "@ar";
print @xxv;
print "\n";
__DATA__
atgacaagcagacccaggggatacgat

I expected output is

    atgacaagcagacccaggggatacgat
    at                  at   at
Preethi Kumar
  • 1,371
  • 1
  • 9
  • 16

4 Answers4

6

To get the desired output through s///

$at ="at";
$ar = <DATA>;
print $ar;
$ar =~ s/$at(*SKIP)(*F)|./ /g;
print $ar;
__DATA__
atgacaagcagacccaggggatacgat

Output:

atgacaagcagacccaggggatacgat
at                  at   at

$at(*SKIP)(*F) expands the variable $at and matches the same set of characters stored in the variable against the input string. Following (*SKIP)(*F) causes the match to fail and forces the pattern on the right side of | to match against the remaining characters. So . matches all the characters except the ones which are skipped. Replacing those matched characters with a space will give you the desired output.

References:

Community
  • 1
  • 1
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thank you for answering, But your answer is not fit for the perl cgi `$ar = join("",@da); print $ar,'
    '; $ar =~ s/at(*SKIP)(*F)|./&nbsp/g; print $ar;` Not correctly fit with the data. How can i do it?
    –  Jan 27 '15 at 10:59
  • do you have the document on the special flag like SKIP and F, I'm unable to look for them on Google. It's the first time I see those flags – lightbringer Jan 28 '15 at 05:38
  • @Avinash raj Thank you for answering . Im using courier which is fit for me. and also use you code. Plese add document about `skip` –  Jan 28 '15 at 07:28
1
use warnings;
use strict;

my $at ="at";
my $ar ="atgacaagcagacccaggggatacgat";

(my $x = $ar) =~ s/./ /g;

substr($x, $-[0], $+[0]-$-[0]-1) = $at while $ar =~ /$at/g;

print "$ar\n$x\n";

As per your comments, it seems that you want to print the result into a webbroser. So, of course, you need a non proportional font. Either you go

 print "<pre>";
 print "$ar\n";
 print "$x";
 print "</pre>";

or you specify monospace:

 print "<div style='font-family:monospace'>");
 print "$ar<br>";
 print "$x";
 print "</div>";
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
  • Thank you for answering, But your answer is not fit for the perl cgi $ar = join("",@da); print $ar,'
    '; $ar =~ s/at(*SKIP)(*F)|./&nbsp/g; print $ar; Not correctly fit with the data. How can i do it?
    –  Jan 27 '15 at 11:28
  • @Rene Nyffenegger I'm remove `-1` from your code, like `($x, $-[0], $+[0]-$-[0])` and run it, also works perfectly. – mkHun Jan 29 '15 at 10:13
0

You can use split with a regex, instead of map.

$data = "atgacaagcagacccaggggatacgat" ;

my @chunks = split(/at/, $data) ;

print $data ; #echo input

#skip over non-matching chunks and print matches
foreach my $chunk (@chunks) {
    print ' ' x length($chunk) ;
    print 'at' ;
}

This will print an extra 'at' at the end, but I am too lazy to clean that up here.

Paul Richter
  • 6,154
  • 2
  • 20
  • 22
  • Thank you for answering, But your answer is not fit for the perl cgi $ar = join("",@da); print $ar,'
    '; $ar =~ s/at(*SKIP)(*F)|./&nbsp/g; print $ar; Not correctly fit with the data. How can i do it?
    –  Jan 27 '15 at 11:26
  • I don't understand why you bring in *cgi* now. – René Nyffenegger Jan 27 '15 at 16:56
0

@user3384402 Previous answers are enough for what you expect.

I think you try to sequence alignment. In the browser visualization that the space is not fit for target it is your problem.

Just do simple, use the courier or courier new font family it is give the correct visual effects. Bioinformatician when handle with the sequence alignment used these fonts only. Try it.

mkHun
  • 5,891
  • 8
  • 38
  • 85