954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Perl

Hallo everyone
im starting to learn perl and i have a problem , i have an exercise asking me to count all the letters each apart in a DNA string , for example
my @DNA =( ctagctagcatgacgatacatgacagataggatacagatagacagatacagatacagatacagatagacccatgacagatac)
so i have to make a perl script showing the user how many t's , a's , c's and g's he typed , what a mean , if the user of the perl script type CTGACTGACGTACGTACGTA , the perl script have to show him how many a's t's c's and g's he typed ....
hoping for help ,,, thanks a lot

wannagethelp
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

try something like:

$tcount;
$acount;
$gcount;
$ccount;
foreach $letter (@DNA)
{
   if (($letter eq "t" ) || ($letter eq "T"))
   {
         $tcount++:
}
and so on
eggmatters
Junior Poster in Training
67 posts since Nov 2008
Reputation Points: 31
Solved Threads: 4
 

Unless you have to use an array you could also do the following.

#!/usr/bin/perl -w
#CountLetters.pl
use strict;
print "Enter string of letters: ";
chomp (my $input = <STDIN>);
my ($char, $count, $verb, $s);
print "\n";
while ($input =~ m/(.)/g) {
    $char = $1;
    #Count and remove all instances of this character -- lower and uppercase
    $count = $input =~ s/$char//gi;
    #Did we find more than one? Singular or plural?
    $verb = $count == 1 ? "is" : "are";
    $s = $count > 1 ? "s" : "";
    print "There $verb $count '$char'$s.\n";
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

$input =~ s/$char/$char/gi would be more appropriate.

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

hi everyone
thnx a lot ,,, but if i want to use a hash ( letter as a key and number as value ) how it would be written ..
thnx again

wannagethelp
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

If you need to store the results in a hash you could do it like this:

#!/usr/bin/perl -w
#CountLetters.pl
use strict;
print "Enter string of letters: ";
chomp (my $input = <STDIN>);
my ($char, $count, $verb, $s, %hash);
print "\n";

while ($input =~ m/(.)/) {
    #When all characters have been counted and replaced with Null,
    # $input will not match /(.)/ and the loop will end
    $char = $1;
    #Count and remove all instances of this character -- lower and uppercase
    #Replace $char with Null so we won't count same character again
    $count = $input =~ s/$char//gi; 
    #Did we find more than one? Singular or plural?
    $verb = $count == 1 ? "is" : "are";
    $s = $count > 1 ? "s" : "";
    print "There $verb $count '$char'$s.\n";
    $hash{$char} = $count; #Store $char as hash key and $count as hash value
}
print "\nThe letter counts have been stored in a hash.\n";
while (($char, $count) = each %hash ){
	print "Hash key '$char' has hash value $hash{$char}\n";
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 
#!/usr/bin/perl
use Data::Dumper;

print "Enter String:\n";
my $string=<STDIN>;
chomp($string);
%hash;
my @a=split(//,$string);
foreach(@a)
{
        if(defined $hash{$_})
        {
                $i=$hash{$_};
                $i++;
                $hash{$_} = $i;
        }
        else{
                $i=1;
                $hash{$_}=$i;
        }

}
print Dumper \%hash;exit;
vbharathi
Newbie Poster
8 posts since Oct 2009
Reputation Points: 13
Solved Threads: 1
 

Also, I think it should be noted that the moderators here frown upon folks writing out the answers to questions in code. Hash implementations and and just about anything else in Perl are pretty well documented online.

eggmatters
Junior Poster in Training
67 posts since Nov 2008
Reputation Points: 31
Solved Threads: 4
 

i am the biggest fan of this forum its so useful for us. thanks for this useful posting i like it very much.

wich
Newbie Poster
3 posts since Oct 2009
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You