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

I have written following code, but its not working properly..

#!usr/bin/perl use strict; my $headrow="<Tr><Tc>At transplantation (POD 0) (n<26>73)<Tc>Early sta +ge (POD 2) (n<26>34)<Tc>Late stage (POD 28) (n<26>25)"; my $str= "<</PICK;t1;0;0;page>> <<tt>>TABLE<ens>1. <mc>Histologic assessment of the liver and islet after transplantation +: comparison among each time points in histologic changes <starttab;1;4><dumline>Islet: cellular infiltration<nx>At transplantat +ion (POD 0) (n<26>73)<nx>Early stage (POD 2) (n<26>34)<nx>Late stage +(POD 28) (n<26>25)<enddum> <Tr><Tc>At transplantation (POD ) (n<26>73)<Tc>Early stage (POD 2) (n< +26>34)<Tc>Late stage (POD 28) (n<26>25) <Tr>Liver: necrosis<Tc>Liver: necrosis<Tc>23/34 (0.68 <22>0.08)<itsup; +a,b><Tc>0/25 (0 <27>0) <Tr>Liver: hypoxia<Tc>Liver: necrosis<Tc>24/34 (0.71 <27>0.08)<itsup;a +,b><Tc>0/25 (0 <27>0) <Tr>Liver: apoptosis<Tc>Liver: necrosis<Tc>223/34 (0.68 <27>0.08)<itsu +p;a,b><itsup;a,b><Tc>0/25 (0 <27>0) <Tr>Islet: cellular infiltration<Tc>Liver: necrosis<Tc>15/34 (0.44 <27 +>0.09)<itsup;a,b><Tc>0/25 (0 <27>0) <Tr>Islet: apoptosis (%)<Tc>Liver: necrosis (POD)<Tc>62.00 - 7.41<itsu +p;a,b><Tc>0 <27>0<endtab> <<tabft>>Histologic change score was indicated as islet numbers accomp +anied with histologic change (necrosis, hypoxia, apoptosis, and cellu +lar infiltration)/total islet numbers (mean score<27>standard error o +f the mean), except islet apoptosis (%). Islet apoptosis (%) was indi +cated as the percentage of apoptotic to total islet cells. <904>n<905 +> is total islet number. <mc><itsup;a>Significant difference (<mdit>P<med><178>0.05), at the ea +rly stage vs. at transplantation. <mc><itsup;b>Significant difference, at the early stage vs. at the lat +e stage. <mc>POD, postoperative day.</.>"; my $tr="<Tr><Tc>At transplantation (POD 0)"; if ($str=~m/$tr/g) { print "$headrow\n"; }

I thought it will print $headrow, but its not printing anything

if i write  my $tr="<Tr><Tc>At transplantation "; It prints $headrow.

And if i write  my $tr="<Tr><Tc>At transplantation ("; I get error for unmatched regex

I dont know why it it happennig.

If i try to mathch $headrow with $str, and try to print something still its not printing anything

Please help Thank you

Replies are listed 'Best First'.
Re: This is Strange!!! Please help me..
by ikegami (Patriarch) on Sep 28, 2010 at 14:22 UTC
    Two errors, one that doesn't affect you yet.
    • What is "if (/.../g)" suppose to mean, "check if it matches, then search the rest of the string to see if it matches too"? It makes no sense, and it will also cause hard to debug errors. Get rid of that "g".

    • "(" and ")" have special meaning in regex patterns. If you want to match a string literally, you'll need to convert the string you want to the match into a pattern that matches that string. quotemeta does that.

      my $tr_pat = quotemeta($tr); if ($str =~ /$tr_pat/) {

      or

      if ($str =~ /\Q$tr\E/) {

      or

      if ($str =~ /\Q$tr) {
Re: This is Strange!!! Please help me..
by Corion (Patriarch) on Sep 28, 2010 at 14:20 UTC

    The opening parenthesis "(" is special in regular expressions. See perlre. Most likely you want to use quotemeta before interpolating a string into a regular expression, or use \Q...\E like this:

    if ($str =~ m/\Q$tr\E/) { ... };

    For stylistic points, the /g on your regular expression serves no purpose.

    To help us help you better, please do not vaguely rephrase Perl error messages but copy and paste the error messages.

      It's not just a stylistic problem.
      >perl -E"$x='a'; for (1..2) { if ($x =~ /a/g) { say 1 } else { say 0 } + }" 1 0
Re: This is Strange!!! Please help me..
by kennethk (Abbot) on Sep 28, 2010 at 14:31 UTC
    Please see How do I compose an effective node title?. A good title lets monks know immediately whether they'll be able to help you. A title such as yours is likely to dissuade monks from helping you.

    And now some issues:

    1. You need to quotemeta since your match string contains regular expression Metacharacters ('(' and ')'). You could also manually escape the characters, but you are more likely to run into trouble with that.

    2. You shouldn't use g on a regular expression (or any Modifiers) unless you need it. In this case, it is a bug but not the bug you were hitting.

    3. And in any case, your string does not contain the term you are searching for. The closest occurrence is <Tr><Tc>At transplantation (POD ), without the '0'.

      There are cases where, although the node title doesn't say anything useful, you still can't complain because the OP also couldn't have invented a better title. This is one of those cases.

      Examine. The body of the post is good: it contains a code example reduced to minimal and explains what the OP expects it to do and how it fails. The minimal code example doesn't use any advanced features: it doesn't include modules (except for strict) or anything. Until you know the answer, there's no better way to describe the question than "I wrote something very simple in perl and it doesn't work".

      Thanks to tye for explaining this phenomenon to me.

        I should have, with my chiding, included positive reinforcement for the well-formatted and well-considered body of the post. I certainly agree with that point and will try to remember in the future.

        My issue is not with the null-statement title, but with the tone. Whenever I see an exclamation point in a title, let alone three, I interpret it the Perl sense - ! (Please help me). Had the title been "This is Strange - Please help me..", I would not have included the gripe (or better yet, simply "This is Strange"). I should have made this point more clearly - if an old-hand such as yourself misinterpreted my intent, then a new member would undoubtedly have done the same.