Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Getting the count of a particular alphabet using subroutine

by Ratazong (Monsignor)
on Aug 06, 2010 at 06:59 UTC ( [id://853330]=note: print w/replies, xml ) Need Help??


in reply to Getting the count of a particular alphabet using subroutine

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

Replies are listed 'Best First'.
Re^2: Getting the count of a particular alphabet using subroutine
by k_manimuthu (Monk) on Aug 06, 2010 at 08:01 UTC

    And another one way

    $string='Put your text'; $count++ while($string=~ m{t}g);
Re^2: Getting the count of a particular alphabet using subroutine
by changma_ha (Sexton) on Aug 06, 2010 at 10:01 UTC

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found