http://www.perlmonks.org?node_id=527101


in reply to 'grouping' substrings?

I have come up with this. I dont know whether using $& is effecient or not.

use strict; use warnings; my $seq="IIIIIMMMMMMMMMMMOOOOOOOOOOOOOOOOMMMMMMMMMMMMMIIIIIMMMMMMMMMOO +OOO +OOOOOOOOOMMMMMMMMMMMMMIIIMMMMMMMMMMMOOOOOOOOOOOOOOOMMMMMMMMMMMIIIIIIM +MMMMMMMMMMMMOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMIIIMMMMMMMMMOOOOOOOOOOOOOOO +OOOOOOOOOOOOMMMMMMMIIIIMMMMMMMMMMMOOOOOOOOOOOOOOOOOOOOOMMMMMMMIIIMMMM +MMMMMOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMIIIMMMMMMMMMMMOOOOOOOOOOOOOOOO +OMMMMMMMMI"; while ($seq=~/(M+)/g) { my $l = pos($seq); print $l-length($&)+1," to ",$l,$/; }

Regards,
Murugesan Kandasamy
use perl for(;;);

Replies are listed 'Best First'.
Re^2: 'grouping' substrings?
by revdiablo (Prior) on Feb 01, 2006 at 16:42 UTC
    I dont know whether using $& is effecient or not.

    Using $& is not efficient, and usually to be avoided. See the entry in perlvar for details.

    Update: perhaps I was a bit imprecise. I'd say something that "imposes a considerable performance penalty on all regular expression matches" is inefficient, but I guess it depends on what type of inefficiency we're talking about.

      That's not exactly true. $& is only inefficient if you have another regexp in your program which doesn't capture.

      However, it's use is discouraged, since captures can perform the same task without the "effect at a distance" of $&.

      In this case, just replace $& with $1, and you're set.