How do I count blocks of characters in a string variable?

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default How do I count blocks of characters in a string variable?

    I'm trying to create a code to count a certain character in a string,
    lets say that I want to know how many letters "o" there are in the
    text "The book is yellow", I'd use the following code:

    $text = "The book is yellow";
    @characters = split (//,$text);
    foreach $char (@characters){
    if ($char eq "o"){
    $count++;
    }
    }

    Very easy, but I think this is a lot of code for such a simple task,
    there's got to be a simpler way to do this, besides, what if I want to
    count a block of characters instead of one only (e.g. how many "el"
    there are in $text)? How do I do this?

    I hope someone can help me with this.
    Researcher Guest

  2. Similar Questions and Discussions

    1. Count Character In String?
      I have various strings that will be returned from a query, but I can to be able to remove the space and replace them with an underscore. For...
    2. Special Characters in Component's String-type variable
      Hi, I have a component that has a string variable which I put text in, using the parameters window... I would like very much to be able to go into...
    3. Count characters in Freehand
      Hello Is it posible to count how many character (words, commas, ) there is in any Freehand document? (For publishing needs) Thank you.. --
    4. count char in string
      hy all, I am trying to find a php function that can count howmany times a specific char is in a string. for example: $string = "Hello world";...
    5. How to Count the Characters of a Field?
      I want to discover how many times the bar (/) appears inside of the field numnota how do I do?
  3. #2

    Default Re: How do I count blocks of characters in a string variable?

    On 6 Sep 2003, Researcher wrote:
    >I'm trying to create a code to count a certain character in a string,
    >lets say that I want to know how many letters "o" there are in the
    >text "The book is yellow", I'd use the following code:
    >
    >$text = "The book is yellow";
    >@characters = split (//,$text);
    >foreach $char (@characters){
    > if ($char eq "o"){
    > $count++;
    > }
    >}
    If you just want to count the number of times a character occurs in a
    string, use the tr/// operator.

    $o_count = $string =~ tr/o//;

    Read 'perldoc perlop' for more on tr///.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

    Jeff 'japhy' Pinyan Guest

  4. #3

    Default Re: How do I count blocks of characters in a string variable?

    Jeff 'japhy' Pinyan wrote:
    >
    > On 6 Sep 2003, Researcher wrote:
    >
    > >I'm trying to create a code to count a certain character in a string,
    > >lets say that I want to know how many letters "o" there are in the
    > >text "The book is yellow", I'd use the following code:
    > >
    > >$text = "The book is yellow";
    > >@characters = split (//,$text);
    > >foreach $char (@characters){
    > > if ($char eq "o"){
    > > $count++;
    > > }
    > >}
    >
    > If you just want to count the number of times a character occurs in a
    > string, use the tr/// operator.
    >
    > $o_count = $string =~ tr/o//;
    >
    > Read 'perldoc perlop' for more on tr///.
    Or if 'o' is in a variable you can do this:

    my $char = 'o';

    $count = () = $string =~ /\Q$char/g;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  5. #4

    Default Re: How do I count blocks of characters in a string variable?

    Researcher wrote:
    > I'm trying to create a code to count a certain character in a
    > string, lets say that I want to know how many letters "o" there are
    > in the text "The book is yellow",
    <snip>
    > what if I want to count a block of characters instead of one only
    > (e.g. how many "el" there are in $text)?
    This method works with both single characters and strings:

    $text = "The book is yellow";
    $count++ while $text =~ /o/g;

    Btw, you asked a FAQ. This is where you should have looked up the
    answer:

    perldoc -q count

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  6. #5

    Default Re: How do I count blocks of characters in a string variable?

    Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<bjdong$hp5o9$1@ID-184292.news.uni-berlin.de>...
    > Researcher wrote:
    > > I'm trying to create a code to count a certain character in a
    > > string, lets say that I want to know how many letters "o" there are
    > > in the text "The book is yellow",
    >
    > <snip>
    >
    > > what if I want to count a block of characters instead of one only
    > > (e.g. how many "el" there are in $text)?
    >
    > This method works with both single characters and strings:
    >
    > $text = "The book is yellow";
    > $count++ while $text =~ /o/g;
    >
    > Btw, you asked a FAQ. This is where you should have looked up the
    > answer:
    >
    > perldoc -q count
    Just as an alternative to the regex methods, a common way in some
    languages of counting all the character types in a string would be to
    use the character code as an array index:

    my $text = "The book is yellow";
    my @a;

    $a[ord(lc $_)]++ for split(//, $text);

    print $a[ord('o')], $/;


    Not an expert, so FWIW.
    Greg Guest

  7. #6

    Default Re: How do I count blocks of characters in a string variable?

    Greg <gdsafford@hotmail.com> wrote in comp.lang.perl.misc:
    > Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message
    > news:<bjdong$hp5o9$1@ID-184292.news.uni-berlin.de>...
    > > Researcher wrote:
    > > > I'm trying to create a code to count a certain character in a
    > > > string, lets say that I want to know how many letters "o" there are
    > > > in the text "The book is yellow",
    > >
    > > <snip>
    > >
    > > > what if I want to count a block of characters instead of one only
    > > > (e.g. how many "el" there are in $text)?
    > >
    > > This method works with both single characters and strings:
    > >
    > > $text = "The book is yellow";
    > > $count++ while $text =~ /o/g;
    > >
    > > Btw, you asked a FAQ. This is where you should have looked up the
    > > answer:
    > >
    > > perldoc -q count
    >
    > Just as an alternative to the regex methods, a common way in some
    > languages of counting all the character types in a string would be to
    > use the character code as an array index:
    Those must be languages without a hash data type.
    > my $text = "The book is yellow";
    > my @a;
    >
    > $a[ord(lc $_)]++ for split(//, $text);
    >
    > print $a[ord('o')], $/;
    In Perl, there is no need to decode the characters first:

    my %a;
    $a{ $_}++ for split //, $text;

    print "$a{ o}\n";

    Re-introduce the lc() if wanted.

    Anno
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139