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
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
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
| [reply] |
|
|
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.
| [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
| [reply] |
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;
| [reply] [d/l] [select] |
|
|
And if XX contains regexp meta characters, only can write:
s/(XX)/$1/g;
| [reply] [d/l] |
|
|
s/XX//g would work just as fine if you don't mind being destructive.
| [reply] [d/l] |