perl string match

Discussion in 'Perl Misc' started by Kelvin, Sep 27, 2004.

  1. Kelvin

    Kelvin Guest

    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";
    }
     
    Kelvin, Sep 27, 2004
    #1
    1. Advertising

  2. 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
     
    Mark Clements, Sep 27, 2004
    #2
    1. Advertising

  3. Kelvin

    Joe Smith Guest

    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
     
    Joe Smith, Sep 27, 2004
    #3
  4. 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
     
    Mark Clements, Sep 27, 2004
    #4
  5. Kelvin

    Oliver S?der Guest

    if ($k =~ /^test/)
    {
    $x=$_;
    }
    (Kelvin) wrote in message news:<>...
    > 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";
    > }
     
    Oliver S?der, Sep 27, 2004
    #5
  6. Kelvin

    Paul Lalli Guest

    "Oliver S?der" <> wrote in message
    news:...
    > (Kelvin) wrote in message

    news:<>...
    > > 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
     
    Paul Lalli, Sep 27, 2004
    #6
  7. Kelvin

    Tore Aursand Guest

    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 <>
    "Writing modules is easy. Naming modules is hard." (Anno Siegel, on
    comp.lang.perl.misc)
     
    Tore Aursand, Sep 27, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    653
  2. Victor
    Replies:
    2
    Views:
    658
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    390
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    503
    Ian Kelly
    Dec 20, 2011
  5. Old Echo
    Replies:
    1
    Views:
    193
    Adam Shelly
    Sep 4, 2008
Loading...

Share This Page