Re: if (match) problem
On 3/10/09 Tue Mar 10, 2009 7:59 AM, Dermot paik...@googlemail.com scribbled: Hi, I am not getting the results that I expect from this test and I am not sure why. If I run the script below I get: 1..3 Line=???/FOO BAR, Name=Joe Smo M=??? ok 1 - handle_name ???/FOO BAR Line=change accordingly /FOO BAR, Name=Foo bar M=change ok 2 - handle_name change accordingly /FOO BAR Line=GEOF KID/FOO BAR, Name=Geoff Kidd M= Use of uninitialized value in concatenation (.) or string at credit_handle.t line 33. not ok 3 - handle_name GEOF KID/FOO BAR # Failed test 'handle_name GEOF KID/FOO BAR' # at credit_handle.t line 16. # Looks like you failed 1 test of 3. The 'GOOF KID' entry is not getting handled correctly (or rather as I want). It is being processed within the initial if() block. It was my understanding that a failed match would mean control would fall to the else statement. If I uncomment the 2 lines below, I can make it would but I suspect there is something I am not seeing here. I have used this construct a lot in the past and it's been fine. The match does not fail. In fact, your regex will always match any string. One of the alternatives is equivalent to /^\?*/, which means zero or more question mark characters at the beginning of the string and will always be true. Change this to /^\?+/ meaning one or more ... and you should get the results you are expecting. The alternative works because after the match $ contains what has matched, in this case the empty string '', which evaluates to false, even though you have actually matched your R.E. --- handle_name.t -- use strict; use warnings; use Test::More tests = 3; my %tests = ( 'FOO BAR/FOO BAR' = ['change accordingly /FOO BAR', 'Foo bar'], 'JOE SMO/FOO BAR' = ['???/FOO BAR',' Joe Smo'], 'GOOF KID/FOO BAR' = ['GOOF KID/FOO BAR', 'Gooff Kid' ], ); foreach my $test (keys %tests) { my $got = handle_name($tests{$test}); ok( $got eq $test, handle_name ${$tests{$test}}[0]); } sub handle_name { my $arryref = shift; my ($line, $name) = ($arryref-[0], $arryref-[1]); $name =~ s/^\s+//; $name =~ s/\s+$//; my $regex = qr/^(change|\?*)/i; # $line =~ /$regex/; # if ($) { if ($line =~ /$regex/ ) { print STDERR Line=$line, Name=$name M=\$\\n; my ($nil, $rest) = ($line =~ /^(change\s+\w+\s?|\?*)(\/.*)/); return(uc($name).$rest); } else { return $line; } } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: if (match) problem
2009/3/10 Jim Gibson jimsgib...@gmail.com: On 3/10/09 Tue Mar 10, 2009 7:59 AM, Dermot paik...@googlemail.com scribbled: Hi, I am not getting the results that I expect from this test and I am not sure why. If I run the script below I get: 1..3 Line=???/FOO BAR, Name=Joe Smo M=??? ok 1 - handle_name ???/FOO BAR Line=change accordingly /FOO BAR, Name=Foo bar M=change ok 2 - handle_name change accordingly /FOO BAR Line=GEOF KID/FOO BAR, Name=Geoff Kidd M= Use of uninitialized value in concatenation (.) or string at credit_handle.t line 33. not ok 3 - handle_name GEOF KID/FOO BAR # Failed test 'handle_name GEOF KID/FOO BAR' # at credit_handle.t line 16. # Looks like you failed 1 test of 3. The 'GOOF KID' entry is not getting handled correctly (or rather as I want). It is being processed within the initial if() block. It was my understanding that a failed match would mean control would fall to the else statement. If I uncomment the 2 lines below, I can make it would but I suspect there is something I am not seeing here. I have used this construct a lot in the past and it's been fine. The match does not fail. In fact, your regex will always match any string. One of the alternatives is equivalent to /^\?*/, which means zero or more question mark characters at the beginning of the string and will always be true. Change this to /^\?+/ meaning one or more ... and you should get the results you are expecting. The alternative works because after the match $ contains what has matched, in this case the empty string '', which evaluates to false, even though you have actually matched your R.E. Thanx Jim. That makes sense. I should have spotted that myself :\ Dp. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: pattern match problem
tao wang wrote: hi, I'm trying to write a script to match a pattern like this: 1.10 I wrote [\d.]+, but this also match with pattern ... , which has no number in it. Could somebody help me with it? I'm new to perl and scripts. thanks a lot. /^\d+\.\d+/ which says anchor at beginning with mulitple numbers then a decimal point then multiple digits again. You could place a $ at end of the set(ie /^\d+\.\d+$/ ) if you are allowing only this setup to entered. Wags ;) ** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: pattern match problem
How about something like this? /\d{0,3}\.\d{0,3}/ which should match 0-3 digits followed by a period followed by 0-3 digits. You should be able to see how you can change it if you want to expand how many digits to capture. You could also try: /\d+\.\d+/ Which matches one or more digits followed by a period followed by one or more digits. This will fail, though, on a number like this: .10 or 1., so it depends on what you want to allow as far as the user entering data. -Original Message- From: tao wang [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 13, 2003 4:39 PM To: [EMAIL PROTECTED] Subject: pattern match problem hi, I'm trying to write a script to match a pattern like this: 1.10 I wrote [\d.]+, but this also match with pattern ... , which has no number in it. Could somebody help me with it? I'm new to perl and scripts. thanks a lot. - tao __ Do you Yahoo!? Yahoo! Shopping - Send Flowers for Valentine's Day http://shopping.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: pattern match problem
tao wang wrote: hi, I'm trying to write a script to match a pattern like this: 1.10 I wrote [\d.]+, but this also match with pattern ... , which has no number in it. Could somebody help me with it? I'm new to perl and scripts. thanks a lot. \d.\d+ \d+.\d+ \d*.\d* \d+\.+\d+ \d+\.*\d+ \d*\.+\d* \d*\.*\d+ etc. You get the idea, not sure what your possible data can be... + == one or more matches * == zero or more matches . == any character \. == dot \d == digit http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: pattern match problem
On Thu, Feb 13, 2003 at 04:39:17PM -0800, tao wang wrote: hi, I'm trying to write a script to match a pattern like this: 1.10 I wrote [\d.]+, but this also match with pattern ... , which has no number in it. Could somebody help me with it? I'm new to perl and scripts. thanks a lot. m/^\d\.\d+/ # should match one digit a . and one or more digits. hth, kent -- To know the truth is to distort the Universe. Alfred N. Whitehead (adaptation) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]