To count the occurence of an alphabet, you can use tr as in the below code,
perl -le '$string=q[ttc actssat tb];$count=$string=~tr/t//;print $coun
+t'
5
~suhail | [reply] [d/l] |
| [reply] |
sub countt{
...
my @arraystr =split(//,$string);#split the string acc. to each al
+phabet and pass it to the array.
foreach my $alpha($string){
if ($alpha =~ /t/i){ # if 't' is found
$count++; #increment the count
return $count; #
}
}
If you want to stick to your own approach, I see two issues with your original code:
- you create an array, but don't use it ... maybe you want to use arraystr in your loop
- inside the loop, you have the return; this ends your subroutine before all elements have been processed; you will want to move the return behind the loop...
HTH, Rata | [reply] [d/l] [select] |
$string='Put your text';
$count++ while($string=~ m{t}g);
| [reply] [d/l] |
| [reply] |
perldoc -q count
| [reply] [d/l] |
Take a look in to index
Regards, Murugesan Kandasamy use perl for(;;);
| [reply] |
The title of this node is very confusing. An "alphabet" is a specific set of characters used for writing natural language, like the Latin alphabet or the Hebrew alphabet (whence the term). I believe the word you're looking for is "character" or "letter": "t" is a character which is a letter (letter being a specific class of written character).
In short, a character is the atomical unit of a string. Characters comonly include digits or "numerical digits" (eg, 1, 2, 3), letters (eg, 'a', 'b', 'ɣ'), punctuation (eg, ':', '#', '!'). Letters and numbers taken together are called "alphanumeric characters", or sometimes "alphanumerics".
Note that this is an issue of natural language, not of programming. Specifically, do not try to map these classes of characters to regular expression character classes without consulting the relevant documentation, especially for the purposes of input validation.
| [reply] |