Velocity Reviews > Perl > Help: Count special words

# Help: Count special words

Amy Lee
Guest
Posts: n/a

 11-27-2008
Hello,

Here's my data.

Chr01 621 A A/G A
Chr01 752 C C/G C
Chr01 969 T C/T T
Chr01 1220 A/G G G
Chr01 1246 A/C C C
Chr01 1263 A/G A/G A
.... ...

And as you see I wanna clean up the lines which the 3rd, 4th and 5th
column has the same letter. For example, A A/G A, this line has the same
letter A. So what I'm going is to count letters with A/T/G/C. However, I
didn't grasp the point to solving this problem. So my problem is what
kinds of function(tr?) or something else could help me fix this?

Best Regards,

Amy

Guest
Posts: n/a

 11-27-2008
Amy Lee <(E-Mail Removed)> wrote:

> I wanna clean up the lines which the 3rd, 4th and 5th
> column has the same letter.

-----------------------------------
#!/usr/bin/perl
use warnings;
use strict;

while ( <DATA> ) {
chomp;
print "\$_ has 3 A's\n" if tr/A// >= 3;
print "\$_ has 3 T's\n" if tr/T// >= 3;
print "\$_ has 3 G's\n" if tr/G// >= 3;
print "\$_ has 3 C's\n" if tr/C// >= 3;
}

__DATA__
Chr01 621 A A/G A
Chr01 752 C C/G C
Chr01 969 T C/T T
Chr01 1220 A/G G G
Chr01 1246 A/C C C
Chr01 1263 A/G A/G A
-----------------------------------

--
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Todd
Guest
Posts: n/a

 11-27-2008

> Amy Lee <(E-Mail Removed)> wrote:
>
> > I wanna clean up the lines which the 3rd, 4th and 5th
> > column has the same letter.

>
> while ( <DATA> ) {
> chomp;
> print "\$_ has 3 A's\n" if tr/A// >= 3;

To make question less boring and pure and to be interested, I'll
present a new question as:

give 2 strings, how to judge whether they have common
character?

or the next question, give 3/4/... strings, what to do?

-Todd

Todd
Guest
Posts: n/a

 11-27-2008

I got a solution for this:
> give 2 strings, how to judge whether they have common

>perl -e '"\$ARGV[0]\0\$ARGV[1]"=~/(\S).*?\0.*?\1/ ? print "true" : print "false"' aaaaaaabcccccccccc eebff

true
/xueweizhong:~/perl/misc (1 jobs)
>perl -e '"\$ARGV[0]\0\$ARGV[1]"=~/(\S).*?\0.*?\1/ ? print "true" : print "false"' aaaaaaabcccccccccc eeff

false

Following this, we can write 3 strings solution naturally, but not for
any number of strings problem.

-Todd

Jürgen Exner
Guest
Posts: n/a

 11-27-2008
Todd <(E-Mail Removed)> wrote:
>To make question less boring and pure and to be interested, I'll
>present a new question as:
>
> give 2 strings, how to judge whether they have common
> character?
>
> or the next question, give 3/4/... strings, what to do?

Yawwwwnnnnn

split() the strings into individual characters, then see "perldoc -q
intersection".

jue

Todd
Guest
Posts: n/a

 11-28-2008

> split() the strings into individual characters, then see "perldoc -q
> intersection".
>

I know it will work, but it'll be very very slow, it seems that we
need to wrap a c code using XSUB.

-Todd