$count = $data =~ m/and/; i have tried a way for the matching operator to count the occurences

I don't think the ordinary match operator (m//) does that. I think it just returns true (it matched) or false (no match) in scalar context. In list context, though, it will return a list consisting of the last item matched by each set of parens, which still isn't what you want, since you want to know how many times a given set of parens matched. However, if you have no parens, then I think it returns a list of all the matches of the whole string. If you assign that to an array, *then* evaluate it in scalar context, you should be able to get a count.

my @matches = $data =~ m/and/; my $count = scalar @matches;

Or, more tersely...

my $count = @{[$data=~m/and/]}
i also tried it with the global modifier /g

You could do that in a loop (see below). Note that s///g and m//g do slightly different things.

For counting individual characters, it is generally considered that tr/// is the fastest way, but of course that won't work for something several chars long. There are, of course, other ways to accomplish what you want...

You can get a good approximation with split...

my $count = (scalar split /and/, $data) -1;

One problem with this approach is that it uses extra memory temporarily; if your string is really really long, that could be an issue. (For a string of any sane length, though, it won't matter.) Also, it causes copying of most of the content of the string (again, temporarily), so if this is going to happen many many times in a nested loop it could perform badly. Perhaps the worst problem is that if your pattern (in the example, /and/) matches at the beginning or end of the string that will throw your count off by one.

There are other ways to do it. For example, you could go back to using m//g in scalar context after all; it returns true once for each match, and you can count by incrementing a pointer.

my $count=0; while ($data=~m/and/g) { ++$count }

$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to Re: a Couple of questions! by jonadab
in thread a Couple of questions! by Nik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.