Testing for regex match in array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Testing for regex match in array.

    Hi: I'm stuck! I've tried a thousand things from forum suggestions...I need to parse the unit from a value and test that it exists based on known units (its not for school, its Jan 1..)

    Code:
    #/usr/bin/perl 
    
    my @unit = qw(mV V mA uA A K m uV nA nV pA pV); 
    
    while( defined($line = <LOGFILE>) )
    {  
       if( $line =~ /d+(\s*\w+)/ )
       {
           # now test if match exists!
           foreach( @validUnits )
           {
    	   if( $_ eq $1)
    	   {
    		 print "FOUND unit : $1\n\n";
    	   }
               else
               {
    		print "UNIT DNE : $1\n\n";
    	   }
    				
    	}
        }

    this ALWAYS returns DNE !!

    even when i add debug prints the result is:


    array el is : mV , $1 is : mV

    UNIT DNE : mV




    I have tried grep, exists, any please help!
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Add these 2 lines just after the shebang line.
    Code:
    use strict;
    use warnings;
    Those 2 pragmas should be in every Perl script you write and will point out several problems in your code.

    Comment

    • dissectcode
      New Member
      • Jul 2008
      • 66

      #3
      thank you. i worked on it more this morning and found that because i included a whitespace character in my regex parameter (\s*\w) the space made it a different word than just the word itself, so eq would never be true.

      Comment

      Working...