Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 28th, 2012, 03:50 PM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 1
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 ;

Reply With Quote
  #2  
Old November 28th, 2012, 04:26 PM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 1
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" ;
                    }
                  }
                } 

Reply With Quote
  #3  
Old November 28th, 2012, 04:30 PM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 29 m 2 sec
Reputation Power: 385
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.

Reply With Quote
  #4  
Old November 28th, 2012, 07:02 PM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 1
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

Reply With Quote
  #5  
Old November 29th, 2012, 01:10 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 29 m 2 sec
Reputation Power: 385
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.

Reply With Quote
  #6  
Old November 29th, 2012, 07:55 PM
testerV testerV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 65 testerV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 35 m 42 sec
Reputation Power: 1
Thanks for you help Laurent!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Using “user input” in regex

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap