From: Dr.Ruud on
Tad McClellan:
> Dr.Ruud:
>> Anno Siegel:

>>> The changes by /x only affect the regex proper. The replacement
>>> part is still an ordinary double-quotish string.
>>
>> OK. I am still trying to think up why it was chosen to not affect the
>> replacement part.
>
> Because spaces are _supposed_ to matter when they are in a string.

There can also be spaces in the regex, and there are several ways to
present them.
I use \s where possible, and also "\x{0020}", "\x{20}", even "[ ]", "\
", depending on the context.

So I still see no reason why unprotected spaces should not be ignored in
the replacement part.
"\x{20}" and "\ " would work fine there too.
Or use a variable with a run of spaces, like $space42 = ' 'x42, and an
o-modifier.

--
Affijn, Ruud (gimme a \X)

"Gewoon is een tijger."

From: Dr.Ruud on
Anno Siegel:

> Redundant constructs are an important indicator *against* the authors
> competence. That's why it is generally a good idea to avoid them.

I generally agree.

The Posting Guidelines say: "Do not provide too much information", so I
did cut down my code to an example of a few lines. But I forgot to toss
the s-modifier.

And now that I have inserted the m- and x-modifiers in all the
appropriate places (I had read that chapter of PBP before but had forgot
about it), I won't even have to do that anymore. :)

--
Affijn, Ruud

"Gewoon is een tijger."

From: Anno Siegel on
Dr.Ruud <rvtol+news(a)isolution.nl> wrote in comp.lang.perl.misc:

> "Gewoon is een tijger."

"Habit is a tiger"?

Yeah, a sleepy one, but when you want him to move he's got teeth.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
From: Dr.Ruud on
Abigail:

> /o only matters if you have a variable inside regexp, and then
> only
> if you encounter the regex more than once with a different value in
> the variable. And then only if you want to keep using the old value.


I have series of substitutions that have to be tried in order on every
line of many files.

To make the code more readable, I can store these substitutions in a
hash (with keys like 'A01' meaning phase A, first substitution).

It is no problem to unloop the code for speed, so it might look like:

$x = s/$re{'A01'}[SRCH]/$re{'A01')[REPL]/gsx; # or /gosx
print STDERR $re{'A01'}[NAME], $x if ($x > $re{'A01'}[MIN]);

$x = s/$re{'A02'}[SRCH]/$re{'A02')[REPL]/gsx;
print STDERR $re{'A02'}[NAME], $x if ($x > $re{'A02'}[MIN]);

(and then dozens more)

If possible, I would like the modifiers to be in $re{'key'}[MODS].
(yes, this is all totally untested code yet)

OK, let me first try and test the alternatives. I still have a few days.

--
Affijn, Ruud

"Gewoon is een tijger."

From: Abigail on
Dr.Ruud (rvtol+news(a)isolution.nl) wrote on MMMMCDXLVIII September
MCMXCIII in <URL:news:dkectf.1bg.1(a)news.isolution.nl>:
-: Abigail:
-:
-: > /o only matters if you have a variable inside regexp, and then
-: > only
-: > if you encounter the regex more than once with a different value in
-: > the variable. And then only if you want to keep using the old value.
-:
-:
-: I have series of substitutions that have to be tried in order on every
-: line of many files.
-:
-: To make the code more readable, I can store these substitutions in a
-: hash (with keys like 'A01' meaning phase A, first substitution).
-:
-: It is no problem to unloop the code for speed, so it might look like:
-:
-: $x = s/$re{'A01'}[SRCH]/$re{'A01')[REPL]/gsx; # or /gosx
-: print STDERR $re{'A01'}[NAME], $x if ($x > $re{'A01'}[MIN]);

The question you should ask here is: does "$re{A01}[SRCH]" change?
And if it does, do you want to keep using the *old* value? If the
answer to both questions is yes, you could use /o (although I would
use qr//). If latter question is answered with 'no', using /o will
make that your program will produce the wrong results. If the first
question is answered with 'no', then using /o doesn't matter.

-: $x = s/$re{'A02'}[SRCH]/$re{'A02')[REPL]/gsx;
-: print STDERR $re{'A02'}[NAME], $x if ($x > $re{'A02'}[MIN]);
-:
-: (and then dozens more)
-:


Suppose you have @lines containing all the lines you want to inspect,
and @regexes with all the regexes (as strings), there is a gigantic
difference between:

for my $line (@lines) {
for my $regex (@regexes) {
$line =~ /$regex/
}
}

and

for my $regex (@regexes) {
for my $line (@lines) {
$line =~ /$regex/
}
}

The first code snippet means that you will be doing

scalar (@lines) * scalar (@regexes)

regex compilations, while in the latter case, you only will be
doing

scalar (@regexes)

compilations. (Except if you have only one regex, then you will be
compiling only once, in both code snippets).


-: If possible, I would like the modifiers to be in $re{'key'}[MODS].
-: (yes, this is all totally untested code yet)

s/(?$re{key}[MODS])$re{key}[SRCH]/$re{key}[REPL]/

ought to do the trick.



Abigail
--
map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: Perl gethostbyname
Next: How to Remove Space in text file