Re: entering regular expressions from the keyboard
Jay Savage schreef: Dr.Ruud: Christopher Spears: #print $regexp; Make that print qr/$regexp/; Not sure where your headed with this. My headed? :) It was an alternative for the commented debug line. First, OP wants to print the input back to the user. And I presume that it is more a developer directed print statement. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: entering regular expressions from the keyboard
On 8/21/07, Dr.Ruud [EMAIL PROTECTED] wrote: Jeff Pang schreef: Christopher Spears: print Enter regular expression: ; chomp(my $regexp = STDIN); $regexp = quotemeta($regexp); Since it specifically asks for a regular expression, I would definitely not do quotemeta(). Exactly. quotemeta() defeats the whole purpose here. We *want* the user to be able to input metacharacters for the match. #print $regexp; Make that print qr/$regexp/; Not sure where your headed with this. First, OP wants to print the input back to the user. it makes sense to do this unmodified, for the most part. Also, qr// doesn't modify the variable, it returns the compiled expression, which is just being thrown away after the print. That means the regex is actually being compiled twice. It probably doesn't, though, make sense to compile the regex before entering the loop, so perhaps something like: chomp(my $regexp = STDIN); print $regexp, \n; $regexp = qr/$regexp/; ... One additional note to Chris: In any case, '$_ =~ \$regxep' is almost certainly not what you're looking for. Since $regexp is a simple scalar and not a reference, your current code is trying to match against something like /SCALAR(0x18231cc)/. HTH, --jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!
Re: entering regular expressions from the keyboard
On 8/23/07, Jay Savage [EMAIL PROTECTED] wrote: That means the regex is actually being compiled twice. It probably doesn't, though, make sense to compile the regex before entering the loop, so perhaps something like: Make that *does* make sense. -- j -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!
Re: entering regular expressions from the keyboard
On Aug 20, 11:28 pm, [EMAIL PROTECTED] (Christopher Spears) wrote: I'm working on the second exercise of the second chapter. I'm supposed to write a program that asks the user to type a regular expression. The program then uses the regular expression to try to find a match in the directory that I hard coded into the program. Here is what I have so far: #!/usr/bin/perl -w use strict; print Enter regular expression: ; chomp(my $regexp = STDIN); #print $regexp; opendir(CPPDIR,/home/io/chris_cpp/) or die Could not open directory: $!; my @allfiles = readdir CPPDIR; closedir CPPDIR; foreach $_(@allfiles){ if ($_ =~ \$regexp){ print $_.\n; } } My problem lies with the matching part. I'm not sure how to use the string that I stored in the $regexp variable as a regular expression. Any hints? Shawn and Jeff each gave you half of the answer. Jeff pointed out that when your pattern is contained in a variable, you should use quotemeta(). This will backslash any metacharacters the variable might contain, so that they match themselves rather than being special in the pattern match (so any periods match periods, rather than any character, plus signs match plus signs, rather than meaning one or more of the previous, etc): $regexp = quotemeta($regexp) And Shawn pointed out that the proper syntax for a pattern match is: $_ =~ /$regexp/ Those two lines should be combined: $regexp = quotemeta($regexp); foreach $_(@allfiles){ if ($_ =~ /$regexp/){ print $_.\n; } } Or, instead of calling quotemeta() explicitly, you can use the \Q and \E escape sequences to do the backquoting within the pattern match itself: foreach $_ (@allfiles) { if ($_ =~ /\Q$regexp\E/) { print $_ . \n; } } Also note that an experienced Perl programmer would either eliminate the $_ whenever it's not needed: foreach (@allfiles) { if (/\Q$regexp\E/) { print $_\n; } } Or would use a better variable name as the loop iterator: foreach my $file (@allfiles) { if ($file =~ /\Q$regexp\E/) { print $file\n; } } Hope that helps, Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: entering regular expressions from the keyboard
Jeff Pang schreef: Christopher Spears: print Enter regular expression: ; chomp(my $regexp = STDIN); $regexp = quotemeta($regexp); Since it specifically asks for a regular expression, I would definitely not do quotemeta(). #print $regexp; Make that print qr/$regexp/; -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
entering regular expressions from the keyboard
Hi! I'm trying to get back into Perl again by working through Intermediate Perl. Unfortunately, the Perl part of my brain has atrophied! I'm working on the second exercise of the second chapter. I'm supposed to write a program that asks the user to type a regular expression. The program then uses the regular expression to try to find a match in the directory that I hard coded into the program. Here is what I have so far: #!/usr/bin/perl -w use strict; print Enter regular expression: ; chomp(my $regexp = STDIN); #print $regexp; opendir(CPPDIR,/home/io/chris_cpp/) or die Could not open directory: $!; my @allfiles = readdir CPPDIR; closedir CPPDIR; foreach $_(@allfiles){ if ($_ =~ \$regexp){ print $_.\n; } } My problem lies with the matching part. I'm not sure how to use the string that I stored in the $regexp variable as a regular expression. Any hints? I'm the last person to pretend that I'm a radio. I'd rather go out and be a color television set. -David Bowie Who dares wins -British military motto I generally know what I'm doing. -Buster Keaton -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: entering regular expressions from the keyboard
-Original Message- From: Christopher Spears [EMAIL PROTECTED] Sent: Aug 21, 2007 11:28 AM To: beginners@perl.org Subject: entering regular expressions from the keyboard Hi! I'm trying to get back into Perl again by working through Intermediate Perl. Unfortunately, the Perl part of my brain has atrophied! I'm working on the second exercise of the second chapter. I'm supposed to write a program that asks the user to type a regular expression. The program then uses the regular expression to try to find a match in the directory that I hard coded into the program. Here is what I have so far: #!/usr/bin/perl -w use strict; print Enter regular expression: ; chomp(my $regexp = STDIN); #print $regexp; $regexp = quotemeta($regexp); See also perldoc -f quotemeta. -- Jeff Pang - [EMAIL PROTECTED] http://home.arcor.de/jeffpang/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: entering regular expressions from the keyboard
Christopher Spears wrote: Hi! I'm trying to get back into Perl again by working through Intermediate Perl. Unfortunately, the Perl part of my brain has atrophied! I'm working on the second exercise of the second chapter. I'm supposed to write a program that asks the user to type a regular expression. The program then uses the regular expression to try to find a match in the directory that I hard coded into the program. Here is what I have so far: #!/usr/bin/perl -w use strict; print Enter regular expression: ; chomp(my $regexp = STDIN); #print $regexp; # from here opendir(CPPDIR,/home/io/chris_cpp/) or die Could not open directory: $!; my @allfiles = readdir CPPDIR; closedir CPPDIR; # try: my @allfiles = glob( '*' ); foreach $_(@allfiles){ if ($_ =~ \$regexp){ # bad regular expression. try: if( /$regexp/ ){ print $_.\n; } } My problem lies with the matching part. I'm not sure how to use the string that I stored in the $regexp variable as a regular expression. Any hints? I'm the last person to pretend that I'm a radio. I'd rather go out and be a color television set. -David Bowie Who dares wins -British military motto Of course we'll win; we're British - another British military motto I generally know what I'm doing. -Buster Keaton -- Just my 0.0002 million dollars worth, Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/