Regex problem

I have the following..

#!/perl -lw
$server = "server";
$status = "Critical";
$critical = "dsmc,,,";
my @data = "dsmc UNIX P3";
      
      foreach $line (@data)
      {
            ($fs, $msg) = $line =~ /(.*?)\s(.*)/;
            chomp($fs);
            if ($fs =~ /$critical/i)
            {
                  print("$server *TEST* $msg $server - $critical $status");
                  exit;       # <--------- It SHOULD exit here, but the regex doesn't appear to match..
            }
      }
      print("$server *TEST* $critical not found in list. No ticket generated.");
      exit;
LVL 1
David AldridgeAsked:
Who is Participating?
 
jmcgOwnerCommented:
I changed one line of your script to use 'index':

#!/perl -lw
$server = "server";
$status = "Critical";
$critical = "dsmc,,,";
my @data = "dsmc UNIX P3";
     
     foreach $line (@data)
     {
          ($fs, $msg) = $line =~ /(.*?)\s(.*)/;
          chomp($fs);
          if ( index( $critical, $fs) >= 0 )  #### I changed this line
          {
               print("$server *TEST* $msg $server - $critical $status");
               exit;
          }
     }
     print("$server *TEST* $critical not found in list. No ticket generated.");
     exit;


==================

As for perversity, I'm not sure how to solve that, since this clearly is just a snippet from some larger problem. The value of $critical seems odd -- what with all those commas -- and it's much more commonly seen that the script contains a pattern that is used to find matches on lines read in rather than the file containing patters that are matched against strings built into the script.
0
 
jmcgOwnerCommented:
Your pattern in $critical has commas in it while your @data does not. That would be enough to prevent a match.
0
 
David AldridgeAuthor Commented:
But if $fs has 'dsmc' as its value, and $critical has 'dsmc,,,' as its value, why doesn't a
regex of $fs =~ /$critical/i   match? I don't want an exact match, just match the string
'dsmc'.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
David AldridgeAuthor Commented:
ok, I figured it out.. duh.. :)  supposed to be $critical =~ /$fs/i  

Thanks anyway!
0
 
jmcgOwnerCommented:
I guess that'll work, but based on what you've shown us so far it's a fairly perverse way to go about it.

Regular expressions are great, but when you're checking for an exact sub-match to a string, you're slightly better off using the 'index' operator.
0
 
David AldridgeAuthor Commented:
Can you show me how you would plug the index operator in the above script? I'm new to perl and am ALWAYS looking for 'less perverse' ways of doing things.  :)

0
 
David AldridgeAuthor Commented:
Yes, I didn't want to post all of the script... its a bit large, so that might account for the oddness. $server, $status and $criical are passed to the script with arguments. @data is read from a text file. One question though.. is the index function not case sensitive?

Thanks for you help and advice!
holein5
0
 
jmcgOwnerCommented:
You're right, I missed the need for a case-insensitive match.

You could do:

  if ( index( lc $critical, lc $fs) >= 0 )

but that may start to lose any performance advantage vs the regular expression match. You could omit the 'lc' operator for any string that you already knew for certain didn't need it, such as a string supplied in the script itself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.