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

try something like:

$tcount;
$acount;
$gcount;
$ccount;
foreach $letter (@DNA)
{
   if (($letter eq "t" ) || ($letter eq "T"))
   {
         $tcount++:
}
and so on

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";
}

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

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

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";
}
#!/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;

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.