#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2

    Using “user input” in regex


    Hi
    Is it possible to use a user inputs as variables for regex?
    $var1, $var2, $var3, $var4, $var5, $var6.
    I made a little sub script to test how it might work.
    I thought I can test inputs for undefined vars and then run if/elsif on defined vars only but I do not know how to do this.
    Could you help me with this?
    Thanks, tester

    Code:

    Code:
    #!/usr/local/bin/perl
     use strict;
     use warnings ;
    
     my $xkey_var     = '111var' ;                     
     my $ykey_var     = '222var' ;                     
     my $parama_ONE   = '333var' ;   
     my $parama_TWO   = '444var' ;           
     my $parama_Three = '555var' ;             
     my $parama_Four  = '666var' ;             
    
    
      my $out = 'C:/Out/testout.txt' ;
               open my $out_fh,'>', $out or die "Can't open $out $!\n" ;
      my $t   = 'C:/Ptest/part.txt' ;
               open my $t_fh,'<', $t or die "Can't open $t $!\n" ;
    
               while (my $line = <$t_fh>)  {
                   chomp $line ;
                           
                    if defined $xkey_var;  
    
                    if  ($line =~/$xkey_var/)  {
                          print $out_fh $line, "\n" ; 
                          next ;
                        }                    
                        else {
                        print "Not found Xkey\n" ;
                        }
                    if defined $ykey_var;  
    
                    if  ($line =~/$ykey_var/)  {
                          print $out_fh $line, "\n" ; 
                          next ;
                        }                    
                        else {
                        print "Not found ykey_var\n" ;
                        }                    
                      }
             close $t_fh ;
             exit ;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2
    It seems this construct is working.
    Is there any better way to do this?
    Thanks, testerV

    Code:
                      while (my $line = <$t_fh>)  {
                           chomp $line ;
                           
                    if (defined ($xkey_var)){  
                     
    
                    if ($line =~/$xkey_var/)  {
                          print $out_fh $line, "\n" ; 
                          next ;
                        }                    
                        else {
                        print "Not found Xkey\n" ;
                        }
                      }
      
                    if (defined ($ykey_var)){  
                     
    
                    if ($line =~/$ykey_var/)  {
                          print $out_fh $line, "\n" ; 
                          next ;
                        }                    
                        else {
                        print "Not found Xkey\n" ;
                        }
                      }
                    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    779
    Rep Power
    495
    These lines:

    Code:
     if defined $xkey_var;
    and

    Code:
    if defined $ykey_var;
    are totally useless. First because the variables are defined at the top of your program and are therefore bound to be defined, so that the if conditional will return true. Second because you you do not take any action on the basis of whether the conditional is true or not. So, just remove these to lines.

    The second point is that if your line matches, you print to a file and if it does not match you print the message to standard output. It may be what you want, but it does not look so, I would think you probably want both cases to be printed to the same location.

    The third point is that if your line matches $xkey_var, the 'next' statement takes you to the next line of the input, so that you don't check whether $ykey_var matches the same line. Again, it may be what you want, but I cannot be sure.

    Finally, when you want to use a variable for storing a regex, it is usually better to do it this way:

    Code:
     my $xkey_var     = qr/111var/ ;
    although it really does not change anything in the case in point.

    EDIT: I did not see your second message when I wrote mine (which was thus aimed at your first post), but it does not really change anything. The two "if defined" statements are useless if you just defined them in your code.
    Last edited by Laurent_R; November 28th, 2012 at 04:33 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2
    Hi Lauren!
    Your answer is much appreciated.
    The script I posted is a test script.
    All my vars coming from the user input :

    Code:
        my $y_key = $mw->Entry(-width=>'13',- borderwidth => '6',- relief=>'sunken', 
        		          - textvariable => \$ykey_var )
    If replace if defined line :
    Code:
                    if (defined ($xkey_var))  {  
                    if ($line =~/$xkey_var/)  {
                          print $out_fh $line, "\n" ; 
                        }
    With :
    Code:
    		 if ($line =~/$xkey_var/)  {
    		       print $out_fh $line, "\n" ; 
    		    }
    The script crashes.
    Thanks again for coaching.
    testerV
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    779
    Rep Power
    495
    OK, sorry, I misunderstood your question on the basis of the code you provided, but you did indeed mention user input.

    If the value is not defined, I think it it should not crash but issue a "undedined" warning, so that if it crash there must be somle other error.

    Otherwise, I would code it somewhat simpler:

    Code:
    if (defined $xkey_var  and $line =~/$xkey_var/)  {
           print $out_fh $line, "\n" ; 
    }
    Last edited by Laurent_R; November 29th, 2012 at 01:14 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2
    Thanks for you help Laurent!

IMN logo majestic logo threadwatch logo seochat tools logo