in reply to count repeatation

The tr =~ trick won't work because you have multiple letters in your pattern.

#!/usr/bin/perl -w use strict; my $string ='ab23cdefgXX(3A5)XXhijkl23mnXX(3)XXopq432rsXX(450b)XXtuv'; my $num = (my @matches) = ($string =~ m/xx/ig); print $num; __END__ prints 6

Replies are listed 'Best First'.
Re^2: count repeatation
by AnomalousMonk (Archbishop) on Aug 19, 2009 at 14:52 UTC
    And use of the goatse 'operator'  =()= eliminates the need for a possibly redundant lexical array variable (matching in this example is case-sensitive):
    >perl -wMstrict -le "my $str ='ab2cgXX(A5)XXhl3mnXX(3)XXo42rsXX(4b)XXv_xx'; my $reps =()= $str =~ m{ XX }xmsg; print $reps; " 6
      Interesting...Never heard the term goatse before! I don't know that the lexical variable is "redundant". I figure that Perl creates it whether it has a name or not! Adding a lexical name to this goatse list appears to be just a minor detail. It could be that there are even shorter ways to formulate this function. I just wrote the first 2 obvious lines of code that came to mind.

      This was not a difficult problem and quite frankly, I would expect the OP to spend more time thinking about it.

        Believe me, you want to leave that term right where it is. It is not a term for polite company, and when viewing the, umm, data behind the term, you may want to gouge out your eyes, rip out your memories by your brain stem, and do all sorts of other purging activities.

        That is all I am going to say *shudder*.

        I, in my more naive days, opened the link from /. on my desktop at work. Thankfully, I was at a more permissive employer at the time. Once was more than enough.

        --MidLifeXis

        The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

        I figure that Perl creates it whether it has a name or not!

        No. The results are placed on the stack in both cases, but no array is created or used when assigning to (). The elements on the stack are simply discarded.

        Independently, since the (rightmost) assignment is evaluated in scalar context, it evaluates to the number of elements returned by its RHS. This occurs whether an array or a list is found on its LHS.

        IIRC, I first encountered the term in a post by merlyn and thought it might be a Randall-ism, perhaps representing an overhead view of two goats butting heads. I've just done a little reasearch and found out that 'butting' is indeed involved: cf. Goatse.cx and goatse if you really gotta know — Update: but first consider the wise counsel of MidLifeXis in Re^4: count repeatation above!

        Maybe now I'll look around for a more "right and proper, m'am" alternative! OTOH, does anyone know a link to a 'proper' definition?

      Aargh! I had almost forgotten. Damn you!

      Just a something something...
Re^2: count repeatation
by ikegami (Patriarch) on Aug 19, 2009 at 17:36 UTC
    Yes and no. You can still use the tr/// trick by using s///.
    my $num = $string =~ s/XX/XX/g;

    It works in list context too.

    print $string =~ s/XX/XX/g;
      And if XX contains regexp meta characters, only can write:
      s/(XX)/$1/g;
        s/XX//g would work just as fine if you don't mind being destructive.