Re: numbers and strings and regex?



Geoff Cox <geoff.cox@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
> On 30 May 2005 16:43:25 GMT, anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno
> Siegel) wrote:
>
> >Geoff Cox <geoff.cox@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
>
> >Your pattern requests files that have an extension ".jmx". Your examples
> >have an extension ".doc" or none. Also, \d{1} is the same as \d.
>
> my mistake they should be the same, ie .jmx
>
> I have changed to \d instead of \d{1} but still something wrong with
> the code
>
> if ($name =~ /.*?(\d{2})-((\d)(\d))\.jmx/i) {
>
> my $both = $2;
> my $first = $3;
> my $second = $4;
>
> if ($4 <= 9)
> { $exnum = $4;

Where is $exnum declared? This doesn't compile under strict. Please make
your code strict-clean.

> }
> else {my $exnum = $both;

Whatever the first $exnum was, this one (declared with "my") is something
else. The assignment here will have no effect outside the else-block,
and it has none inside. You might as well drop the else-block. Apparently
not what you intended.

> }
>
> the $exnum value for when the second pair of digits in 01-11 is
> greater than 09 ...?

Which $exnum are we speaking of?

Since your code does things you didn't intend it does a poor job of
communicating what you did intend. Write a complete, self-contained
program that shows some output. Then explain how the output is not
what you expect.

I had to rework your code into something runnable anyway, so I'm appending
it (as a signature) to show what I mean. Add a suitable declaration to
$exnum. If it still doesn't do what you think it should do, point to where
its output is different from what you think it should be.

Anno

--
#!/usr/local/bin/perl
use strict; use warnings; $| = 1;

my @strings = qw(
blue-green-01-01.jmx
blue-green-01-09.jmx
blue-green-01-11.jmx
blue-green-01-32.jmx
);

for my $name ( @strings ) {

if ($name =~ /.*?(\d{2})-((\d)(\d))\.jmx/i) {

my $both = $2;
my $first = $3;
my $second = $4;

our $exnum;
if ($4 <= 9) {
$exnum = $4;
} else {
my $exnum = $both;
}
print "$name -> first: $first, second: $second, both: $both, " .
"exnum: $exnum\n";
}
}
__END__
.