Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

perl string match
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";
}

Report this thread to moderator Post Follow-up to this message
Old Post
Kelvin
09-27-04 08:56 AM


Re: perl string match
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

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Clements
09-27-04 01:58 PM


Re: perl string match
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

Report this thread to moderator Post Follow-up to this message
Old Post
Joe Smith
09-27-04 01:58 PM


Re: perl string match
Xref: number1.nntp.dca.giganews.com comp.lang.perl.misc:551897

Joe Smith wrote:
> Mark Clements wrote:
> 
>
>
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Clements
09-27-04 01:58 PM


Re: perl string match
if ($k =~ /^test/)
{
$x=$_;
}
kcassidy@kakelma.mine.nu (Kelvin) wrote in message news:<26b862d9.0409262250.22e46f72@posti
ng.google.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";
>  }

Report this thread to moderator Post Follow-up to this message
Old Post
Oliver S?der
09-27-04 01:58 PM


Re: perl string match
"Oliver S?der" <osoeder@gmx.de> wrote in message
news:45c6c8e6.0409270408.67e5eec1@posting.google.com...
> kcassidy@kakelma.mine.nu (Kelvin) wrote in message
news:<26b862d9.0409262250.22e46f72@posting.google.com>... 
$x="test") 
>
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
09-27-04 01:58 PM


Re: perl string match
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 <tore@aursand.no>
"Writing modules is easy. Naming modules is hard." (Anno Siegel, on
comp.lang.perl.misc)

Report this thread to moderator Post Follow-up to this message
Old Post
Tore Aursand
09-27-04 09:01 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:29 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.