From: Paul Tremblay <[EMAIL PROTECTED]>
> I am writing a script to parste rtf into xml and have a number of
> questions about regular expressions.
>
> The first is:
>
> I have this line:
>
> \sbknone\linemod0\linex0\cols1\endnhere \pard\plain text\par
>
> I want to split it at the expression "\endnhere". I can either
> use
>
> @array = split(/endnhere/,2);
> $first_part_of_line = $array[0];
> $second_part_of_line = $array[1];
This may be written
($first_part_of_line, $second_part_of_line)
= split(/endnhere/,2);
> Or:
>
> $line=~/(.*?)endnhere(.*);
> $first_part_of_line = $1;
> $second_part_of_line = $2;
>
> Which is quickest? I am using this method repeatedly in my
> script, so I wanted the quickest method.
[rem]
Give a man a fish and you'll feed him for one day,
teach him how to fish and he'll die of hunger 'cause there'll be no
fishes left with all those fishers.
[/rem]
See
perldoc Benchmark
you can measure what's quicker and how big is the difference yourself
:-)
I would not expect any big difference though.
Maybe this could be quicker
{
my $i = index $line, 'endnhere';
if ($i != -1) {
$first_part_of_line = substr $line, 0, $i;
$second_part_of_line = substr $line, $i+8;
}
}
This way we do not use regexps.
> I also have come accross the g anchor. For example:
>
> $line=~/(.*?)endnhere/g;
> $rest_of_line=~/\G.*/;
>
> Is it advisable to use this anchor? I know that the $` and $' are
> deprecated because they slow down a script (at least according to
> *Perl Cookbook*). How about these anchors? They seem like they would
> be very useful.
Since there's no remark about it causing a slowdown in either perlre
nor perlop I'd think \G is OK. And as far as I understand what is it
doing there is no reason it should slow anything down.
Jenda
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
--- me
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]