On Jul 27, 2012, at 7:04 AM, Gary Stainburn wrote:

> Hi folks.
> 
> I'm struggling to see what I'm doing wrong.  I have the following code in one 
> of my programs but it isn't working as it should.
> 
> 
> print STDERR "enqmake='$enqmake' model='$model'\n";
> if (!$enqmake && $model) { # extract make
>  print STDERR "About to split '$model'\n";
>  if ($model=~/ *?(\w*) (.*?) *$/) {
>    $enqmake=lc($1);
>    $model=$2;
>    print STDERR "model split into '$enqmake' '$model'\n";
>  }
> } # extract make
> 
> This generates:
> 
> enqmake='' model='Kia Venga'
> About to split 'Kia Venga'
> 
> I have a test script which works fine. Can anyone see what I'm doing wrong?

No. Your script works fine for me if I precede it with the following two lines:

my $model = 'Kia Venga';
my $engmake;

> 
> #!/usr/bin/perl -w
> 
> use warnings;
> use strict;
> 
> my $t='Kia Venga';
> 
> if ($t=~/ *?(\w*) (.*?) *$/) {
>  print "1='$1' 2='$2'\n";
> }
> 
> [root@ollie exim]# ~/t
> 1='Kia' 2='Venga'

Your test script also works fine. Therefore, it must be something else in your 
larger program.

I suggest you use the escape sequence \s for whitespace instead of just using 
the space character. You should also use the x modifier so that spaces in your 
pattern will be ignored. That will allow you to determine by inspection what 
your pattern is really doing. I also use m{ } to delineate the pattern and \z 
to anchor the end of match instead of $:

if( $model =~ m{ \s*? (\w*) \s (.*?) \s* \z }x ) {
  ...

Why aren't you using the split function?

($model,$engmake) = split(' ',$model);


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to