Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl string match

Reply
Thread Tools

perl string match

 
 
Kelvin
Guest
Posts: n/a
 
      09-27-2004
Hi All,

Need a bit of help in pattern matching How to accomplish the
code below of the string "test" is inside a variable? (e.g. $x="test")



if ($k =~ /^test/)
{
print "1\n";
}
 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      09-27-2004
Kelvin wrote:
> Hi All,
>
> Need a bit of help in pattern matching How to accomplish the
> code below of the string "test" is inside a variable? (e.g. $x="test")
>
>
>
> if ($k =~ /^test/)
> {
> print "1\n";
> }

my $searchString = "test";
if ( $k =~ /^$searchString/) {


}

man perlop

also check out the /o switch

Mark
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      09-27-2004
Mark Clements wrote:

> man perlop
>
> also check out the /o switch


Hmmm. 'perldoc perlop' does not state whether the lack of /o has as
much of a performance impact in perl-5.8 as it had in earlier versions.
-Joe
 
Reply With Quote
 
Mark Clements
Guest
Posts: n/a
 
      09-27-2004
Joe Smith wrote:
> Mark Clements wrote:
>
>> man perlop
>>
>> also check out the /o switch

>
>
> Hmmm. 'perldoc perlop' does not state whether the lack of /o has as
> much of a performance impact in perl-5.8 as it had in earlier versions.
> -Joe

a quick test with 5.8 on Solaris 9 shows very little difference.

use strict;
use warnings;

use Benchmark::Timer;

my $searchExpression=shift;
my $text=shift;
my $iterations=shift;

my $timer=Benchmark::Timer->new();
$timer->start("overall");
for(my $ii=0;$ii<$iterations;$ii++){
$timer->start("iteration");
$text=~/$searchExpression/;
$timer->stop("iteration");
$timer->start("iterationwitho");
$text=~/$searchExpression/o;
$timer->stop("iterationwitho");
}
$timer->stop("overall");

$timer->report();



redwood 24170 $ perl ./timere.pl ^asdf thisissometext 10000
1 trial of overall (1.014s total)
10000 trials of iteration (100.802ms total), 10us/trial
10000 trials of iterationwitho (98.526ms total), 9us/trial

redwood 24171 $ perl ./timere.pl ^asdf thisissometext 10000
1 trial of overall (1.011s total)
10000 trials of iteration (100.544ms total), 10us/trial
10000 trials of iterationwitho (96.909ms total), 9us/trial

redwood 24172 $ perl ./timere.pl ^asdf thisissometext 10000
1 trial of overall (938.571ms total)
10000 trials of iteration (93.197ms total), 9us/trial
10000 trials of iterationwitho (89.684ms total), 8us/trial

assuming my test is correct...

Mark
 
Reply With Quote
 
Oliver S?der
Guest
Posts: n/a
 
      09-27-2004
if ($k =~ /^test/)
{
$x=$_;
}
http://www.velocityreviews.com/forums/(E-Mail Removed) (Kelvin) wrote in message news:<(E-Mail Removed). com>...
> Hi All,
>
> Need a bit of help in pattern matching How to accomplish the
> code below of the string "test" is inside a variable? (e.g. $x="test")
>
>
>
> if ($k =~ /^test/)
> {
> print "1\n";
> }

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-27-2004

"Oliver S?der" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> (E-Mail Removed) (Kelvin) wrote in message

news:<(E-Mail Removed). com>...
> > Need a bit of help in pattern matching How to accomplish the
> > code below of the string "test" is inside a variable? (e.g.

$x="test")
> >
> > if ($k =~ /^test/)
> > {
> > print "1\n";
> > }

>
> if ($k =~ /^test/)
> {
> $x=$_;
> }


Please post your reply below what you are replying to.

Can you please explain exactly what it is you think this code is doing?

Paul Lalli


 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      09-27-2004
On Sun, 26 Sep 2004 23:50:04 -0700, Kelvin wrote:
> Need a bit of help in pattern matching How to accomplish the
> code below of the string "test" is inside a variable? (e.g. $x="test")
>
> if ($k =~ /^test/)
> {
> print "1\n";
> }


If you just need to find out if a variable is inside another variable, you
shouldn't use regular expressions. This will do (and it's faster);

my $k = 'This is a test';
my $x = 'test';

if ( index($k, $x) >= 0 ) {
# Match
}

Eventually, you can check if 'index(...)' equals 0 if you want to check if
the string begins with $x.


--
Tore Aursand <(E-Mail Removed)>
"Writing modules is easy. Naming modules is hard." (Anno Siegel, on
comp.lang.perl.misc)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments