Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Counting the occurance of an element ian array

Posted by naveen (naveen), 3 June 2008
hello everyone..
suppose my motif is TER..
i have a pdb file in whish all the words like header,atom,water,etc are the elements of the array..
 i Want to search how many times TER occurs as the element of the array..

m using the followin code
$b=0;
my($motif1) = 'TER';
for($a=0;$a<$len1;$a++)
{
if($len[$a] =~/$motif1/)
{
$b++;
}
}
print"\n\n";
print $b,"\n";

Posted by KevinAD (KevinAD), 3 June 2008
What is not working in your code? It is better written like so:


my $r = 0;
my $motif1 = 'TER';
for (@array) {
  $r++ if /$motif1/;
}

print "\n\n$r\n";


Which will count occurances of 'TER' but not 'ter', add the "i" option to the end of the regexp to make it case insensitive:


my $r = 0;
my $motif1 = 'TER';
for (@array) {
  $r++ if /$motif1/i;
}

print "\n\n$r\n";






This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho