J
J Krugman
1. Supposed I wanted apply s/// at a particular (offset, length)
substring of a string (for example, applying s/(a[^a]+)/*\U$1*/ to the
(offset = 7, length = 3) substring in 'abracadabra', to get
'abracad*ABR*a'). I could use some labored code like this:
$string = 'abracadabra';
($substring = substr($string, 7, 3)) =~ s/(a[^a]+)/;
substr($string, 7, 3) = $substring;
This requires creating an auxiliary variable $substring and two calls
to substr, which seems like a waste. Is there any way to target s///
to a particular substring in a string?
2. Given any finite string S and given any regexp R, there is a finite
set A of (o, w) pairs, such that the substring S[o, w] of S, beginning
at offset o and having length w matches R [1]. For example, if
S = '1a21b4xy' and
R = /(\d+)/,
then the pairs in the set A would be
(0, 1) --> '1'
(2, 1) --> '2'
(2, 2) --> '21'
(3, 1) --> '1'
(5, 1) --> '4'
Does Perl offer any simple and/or built-in way to generate the set
A?
2.5 There's a *different* problem, simpler for me to code than the one
described in (2): generate the set B of all pairs (length($PREMATCH),
length($MATCH)) generated during a "global" (i.e. /g-modified) match.
For example:
while ($S =~ /(\d+)/g) {
push @pairs, [ map length, ($`, $&) ];
}
For the string S and the regexp R in the example given in question 2,
the set B would be { (0, 1), (2, 2), (5, 1) }.
Does Perl offer some built-in mechanism to get directly at the set B?
TIA,
jill
[1] If we changed R in (2) to the anchored regexp /^(\d+)/ we would
get the same set A as before, and not the set { (0, 1) }, because each
substring S[o, w] is tested against R in isolation.)
substring of a string (for example, applying s/(a[^a]+)/*\U$1*/ to the
(offset = 7, length = 3) substring in 'abracadabra', to get
'abracad*ABR*a'). I could use some labored code like this:
$string = 'abracadabra';
($substring = substr($string, 7, 3)) =~ s/(a[^a]+)/;
substr($string, 7, 3) = $substring;
This requires creating an auxiliary variable $substring and two calls
to substr, which seems like a waste. Is there any way to target s///
to a particular substring in a string?
2. Given any finite string S and given any regexp R, there is a finite
set A of (o, w) pairs, such that the substring S[o, w] of S, beginning
at offset o and having length w matches R [1]. For example, if
S = '1a21b4xy' and
R = /(\d+)/,
then the pairs in the set A would be
(0, 1) --> '1'
(2, 1) --> '2'
(2, 2) --> '21'
(3, 1) --> '1'
(5, 1) --> '4'
Does Perl offer any simple and/or built-in way to generate the set
A?
2.5 There's a *different* problem, simpler for me to code than the one
described in (2): generate the set B of all pairs (length($PREMATCH),
length($MATCH)) generated during a "global" (i.e. /g-modified) match.
For example:
while ($S =~ /(\d+)/g) {
push @pairs, [ map length, ($`, $&) ];
}
For the string S and the regexp R in the example given in question 2,
the set B would be { (0, 1), (2, 2), (5, 1) }.
Does Perl offer some built-in mechanism to get directly at the set B?
TIA,
jill
[1] If we changed R in (2) to the anchored regexp /^(\d+)/ we would
get the same set A as before, and not the set { (0, 1) }, because each
substring S[o, w] is tested against R in isolation.)