rsennat has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
$row{test_name} = '{CHK.test: 1 2 3}'; if( $row{test_name} =~ "CHK" ) { print "CHK-------"; }
This code is not printing "CHK---". But if I remove the curly braces in the string, it works fine. The string cannot be changed but still i need to check for the pattern there.

How to do this??
thanks
rsennat

Replies are listed 'Best First'.
Re: problem in search pattern
by davido (Cardinal) on Dec 16, 2005 at 10:56 UTC
    $row{test_name} = '{CHK.test: 1 2 3}'; if( $row{test_name} =~ "CHK" ) { print "CHK-------"; }

    Rule #1: when you post code and say it doesn't work, make sure that the code you post actually doesn't work. What you posted does print "CHK-------", as it should.


    Dave

Re: problem in search pattern
by GrandFather (Saint) on Dec 16, 2005 at 10:59 UTC
    use strict; use warnings; my %row; $row{test_name} = '{CHK.test: 1 2 3}'; if( $row{test_name} =~ "CHK" ) { print "CHK-------"; }

    Prints:

    CHK-------

    for me. How is your code different from the code that I copied from your post and added a couple of lines to for the sake of tidyness?


    DWIM is Perl's answer to Gödel
Re: problem in search pattern
by blazar (Canon) on Dec 16, 2005 at 11:25 UTC
    I see no reason why this should not work. But, even though the string CHK is converted to a pattern for you, it is a bad idea to do so, and you should use a pattern yourself in the first place. There's only one case in which a string used there has an actual, "special" semantic meaning, and it's not this one...
Re: problem in search pattern
by blazar (Canon) on Dec 16, 2005 at 11:31 UTC
    if( $row{test_name} =~ "CHK" ) { print "CHK-------"; }
    This code is not printing "CHK---".
    Indeed, it prints "CHK-------"! Please note that this is not only a joke: I'm tring to draw your attention to the need to be very careful about checking what you write, to establish a fruitful communication.
Re: problem in search pattern
by rsennat (Beadle) on Dec 16, 2005 at 13:26 UTC
    Actually i modified the problem and posted it here to make it simple. But that worked. But here goes the actual problem, the string values will be,
    $row{test_name} will contain any of these values, "BEFORE-AFTER.etest" "DEFAULT.etest" "ORDER-ROLLBACK.etest" "{PRCIII.etest: logging size 100}" "ROLLBACK.etest" "SIDE-EFFECTS.etest" "SYNTAX.etest" #then i check for PRC if( $row{test_name} =~ "PRC" ) { print "PRC-------"; }
    Really this problem is there only with PRC, which is in CURLY braces.
    Or can you tell me how to remove the starting and trailing curly braces.

    thanks
    rsennat
      As someone said earlier, you want to say:
      $row{test_name} =~ /PRC/
      rather than:
      $row{test_name} =~ "PRC"
      Note the slashes versus quote marks.

      Your data and code still prints the wanted result for me.

      Do you realise you still haven't shown us the part of the code where your error is to be found?

      We are better at tracking errors in code we can see --- than at guessing what might be wrong in the code you do not show.

      Cheers, Sören

Re: problem in search pattern
by rsennat (Beadle) on Dec 16, 2005 at 14:58 UTC
    Exact data and code. Please help me to resolve this string manipulation. Really some problem still hinders me.
    $VAR1 = { 'cli_config' => 'logging size', 'jobid' => '982505', 'status +' => 'skip', 'component1' => 'archive-config-log', 'test_name' => 'BE +FORE-AFTER.etest', 'testcase_name' => 'TC11327641669.xml', 'color_boo +l' => 0 }; $VAR1 = { 'cli_config' => 'logging size', 'jobid' => '9825 +05', 'status' => 'skip', 'component1' => 'archive-config-log', 'test_ +name' => 'DEFAULT.etest', 'testcase_name' => 'TC11327641669.xml', 'co +lor_bool' => 1 }; $VAR1 = { 'cli_config' => 'logging size', 'jobid' = +> '982505', 'status' => 'skip', 'component1' => 'archive-config-log', + 'test_name' => 'ORDER-ROLLBACK.etest', 'testcase_name' => 'TC1132764 +1669.xml', 'color_bool' => '' }; $VAR1 = { 'cli_config' => 'logging s +ize', 'jobid' => '982505', 'status' => 'pass', 'component1' => 'archi +ve-config-log', 'test_name' => '{PRCIII.etest: logging size 200}', 't +estcase_name' => 'TC11327641669.xml', 'color_bool' => 1 }; $VAR1 = { +'cli_config' => 'logging size', 'jobid' => '982505', 'status' => 'ski +p', 'component1' => 'archive-config-log', 'test_name' => 'ROLLBACK.et +est', 'testcase_name' => 'TC11327641669.xml', 'color_bool' => '' }; $ +VAR1 = { 'cli_config' => 'logging size', 'jobid' => '982505', 'status +' => 'skip', 'component1' => 'archive-config-log', 'test_name' => 'SI +DE-EFFECTS.etest', 'testcase_name' => 'TC11327641669.xml', 'color_boo +l' => 1 }; $VAR1 = { 'cli_config' => 'logging size', 'jobid' => '9825 +05', 'status' => 'skip', 'component1' => 'archive-config-log', 'test_ +name' => 'SYNTAX.etest', 'testcase_name' => 'TC11327641669.xml', 'col +or_bool' => '' };

    In total 7 hashes. I loop through for every hash and search for the key, "test_name" and parsing the data.

    for my $j(0..$len - 1 ) { my ($component1,$cli_config,$testcase_name,$test_id, $test_name, $status) = split (/\,/,$test_status[$j]); my %row = (); %row = ( component1 => $component1, cli_config => $cli_config, testcase_name => $testcase_name, test_name => "$test_name", ### This IS THE ONE status => $status, jobid => $jobid, color_bool => $color); print Dumper \%row; ##### THIS DATA SHOWN ABOVE if( $row{test_name} =~ "BEFORE-AFTER" ) { push(@loop6,\%row); } elsif( $row{test_name} =~ "DEFAULT" ) { push(@loop7,\%row); } elsif( $row{test_name} =~ "ORDER-ROLLBACK" ) { push(@loop8,\%row); } elsif( $row{test_name} =~ "PRCIII" ) { print "PRCIII------"; push(@loop9,\%row); } elsif( $row{test_name} =~ "ROLLBACK" ) { push(@loop10,\%row); } elsif( $row{test_name} =~ "SIDE-EFFECTS" ) { push(@loop11,\%row); } elsif( $row{test_name} =~ "SYNTAX" ) { push(@loop12,\%row); } }

    All other "test_name" i could parse. But not the "PRCIII". Please help me out.
    Thanks
    rsennat

      Update 2: I re-ran your programme with the "real" life data and it works fine again. The error you are looking for is not in the code you posted.

      Maybe you don't see the print output because it is redirected somewhere you didn't expect?

      Update: I searched for the string in your data, and my browser told me it wasn't there... now I see it. Whatever went wrong.

      In your Dumper output of the test data, there is no string that matches "PRCIII".

      That in turn makes your test for the string "PRCIII" fail.
      In order to make it succeed, put that string in your test data.

      Cheers, Sören

        Yeah right. Atleast you got it.
        "PRCIII" is there in the data.
        Please help me out of this. This really screws me.

        thanks
        Can you please share your code.

        Really something goes wrong here. unable to figure it out. i tried chopping, removing curly braces etc... nothing worked.