Professional Web Applications Themes

How to calculate the number of match occurrences directly? - PERL Miscellaneous

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....

  1. #1

    Default How to calculate the number of match occurrences directly?

    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 Guest

  2. #2

    Default Re: How to calculate the number of match occurrences directly?

    Long Li <com.cn> wrote in comp.lang.perl.misc: 

    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 Guest

  3. #3

    Default Re: How to calculate the number of match occurrences directly?

    Long Li wrote: 

    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 Guest

  4. #4

    Default Re: How to calculate the number of match occurrences directly?

    Long Li graced us by uttering: 

    Either of:

    $num_a++ while $string =~ m/a/g;

    $num_a = $string =~ tr/a/a/;

    The former will match once for each occurence of 'a' in $string,
    each subsequent attempt starting where the former left off.

    The latter actually replaces the 'a' with itself for as many
    times as it's found, thus $string remains identical, while the
    tr/// operator returns the number of (harmless) replacements
    made. This may have some unforeseen drawbacks, however, due to
    modifying $string. FYI.

    I don't have time to benchmark these, but you might try running
    both of these algorithms on very large strings, since you said
    $string might get quite large, using the Benchmark module to time
    them.

    HTH,
    Tim Hammerquist
    --
    You just can't trust a house.
    -- May Kasahara, "The Wind-Up Bird Chronicle"
    Tim Guest

Similar Threads

  1. calculate number of checked boxes in datagrid column
    By lak25 in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: December 23rd, 06:09 PM
  2. query match for phone number
    By pflynn02 in forum Coldfusion - Getting Started
    Replies: 0
    Last Post: March 13th, 03:22 AM
  3. Replies: 2
    Last Post: March 1st, 10:30 PM
  4. How get number of occurrences in a variable?
    By malcolm k in forum Microsoft SQL / MS SQL Server
    Replies: 0
    Last Post: June 30th, 03:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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