How to calculate the number of match occurrences directly?

Discussion in 'Perl Misc' started by Long Li, Oct 15, 2003.

  1. Long Li

    Long Li Guest

    Hi,
    I want to calculate the number of match occurrences, for examples,

    $string = "abcdabcd";
    @a = $string =~ m/a/g;
    $num_a = @a;

    However, I do not want to use the array "@a" because I do not care
    that array, espcially "$string" is very large and include many "a", I
    just want to know the number of "a" occurrences. I tried

    $num_a = $string =~ m/(a)/g;
    or
    $num_a = ( $string =~ m/a/g );

    They both did not work. Please give me some suggestions, thanks.
    Long Li, Oct 15, 2003
    #1
    1. Advertising

  2. Long Li

    Anno Siegel Guest

    Long Li <> wrote in comp.lang.perl.misc:
    > Hi,
    > I want to calculate the number of match occurrences, for examples,
    >
    > $string = "abcdabcd";
    > @a = $string =~ m/a/g;
    > $num_a = @a;
    >
    > However, I do not want to use the array "@a" because I do not care
    > that array, espcially "$string" is very large and include many "a", I
    > just want to know the number of "a" occurrences. I tried
    >
    > $num_a = $string =~ m/(a)/g;
    > or
    > $num_a = ( $string =~ m/a/g );
    >
    > They both did not work. Please give me some suggestions, thanks.


    There's a trick to that:

    $num = () = /a/g;

    The "()" provide list context to the regex, but the actual variable
    (a scalar) is assigned the number of elements. Capturing parentheses
    are not necessary.

    If you only ever want to count single characters, tr/// is simpler
    and more efficient:

    $num = tr/a//;

    Anno
    Anno Siegel, Oct 15, 2003
    #2
    1. Advertising

  3. Long Li wrote:
    > I want to calculate the number of match occurrences, for examples,
    >
    > $string = "abcdabcd";
    > @a = $string =~ m/a/g;
    > $num_a = @a;
    >
    > However, I do not want to use the array "@a" because I do not care
    > that array, espcially "$string" is very large and include many "a",
    > I just want to know the number of "a" occurrences. I tried
    >
    > $num_a = $string =~ m/(a)/g;
    > or
    > $num_a = ( $string =~ m/a/g );
    >
    > They both did not work. Please give me some suggestions, thanks.


    You are asking a FAQ.

    perldoc -q count

    Please consult the FAQ, thanks.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shabam
    Replies:
    2
    Views:
    2,023
    Joe Smith
    Nov 17, 2004
  2. Replies:
    4
    Views:
    755
    Malcolm
    Oct 2, 2005
  3. C3
    Replies:
    3
    Views:
    118
    Brian McCauley
    Sep 29, 2004
  4. PerlFAQ Server
    Replies:
    0
    Views:
    164
    PerlFAQ Server
    Jan 4, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    151
    PerlFAQ Server
    Apr 22, 2011
Loading...

Share This Page