Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Getting the count of a particular alphabet using subroutine

by changma_ha (Sexton)
on Aug 06, 2010 at 05:15 UTC ( #853315=perlquestion: print w/replies, xml ) Need Help??

changma_ha has asked for the wisdom of the Perl Monks concerning the following question:

Hi all..I got struck in a problem. I have wrote a code where using a subroutine i have to count the occurrence of "t". Here is my code

#! /usr/bin/perl use strict; use warnings; sub countt{ my $string =<STDIN>; my $count = 0; $string =~ s/\s*\n//g; # removes the spaces and newline character. 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; } } } #my @list = qw (here is the man in the rain); print &countt;
plz help me out.. thanx in advance

Replies are listed 'Best First'.
Re: Getting the count of a particular alphabet using subroutine
by suhailck (Friar) on Aug 06, 2010 at 06:08 UTC
    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

      Thanx suhailck... now my problem is solved.

Re: Getting the count of a particular alphabet using subroutine
by Ratazong (Monsignor) on Aug 06, 2010 at 06:59 UTC
    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

      And another one way

      $string='Put your text'; $count++ while($string=~ m{t}g);

      Thanx Ratazong..... i made a silly mistakes while writing by not passing an array...now my probs is solved.

Re: Getting the count of a particular alphabet using subroutine
by toolic (Bishop) on Aug 06, 2010 at 12:03 UTC
Re: Getting the count of a particular alphabet using subroutine
by murugu (Curate) on Aug 06, 2010 at 06:04 UTC
    Take a look in to index

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: Getting the count of a particular alphabet using subroutine
by Anonymous Monk on Aug 06, 2010 at 14:47 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://853315]
Approved by murugu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-12-07 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (34 votes). Check out past polls.

    Notices?