On Sunday, January 18, 2004, at 02:38 PM, Andy Turner wrote:


There some sort of regexp strangeness going on here that I can't grok.
Your script doesn't work for me unless I print out the values of $1 AND $2.
If I just print out one it doesn't work either.


turner:~$ cat foo.pl
$_ = "UA-UI1,3,4,6";
m~(..)\-(..)(\d.+)~;
for $CC( $1 .. $2 ) { for $n ( split /,/, $3 ) { print "$CC$n "} }
turner:~$ perl foo.pl
01 03 04 06 turner:~$

But oddly, it works if I add a little debuging:

turner:~$ cat foo.pl
$_ = "UA-UI1,3,4,6";
m~(..)\-(..)(\d.+)~;
print "$1, $2\n";
for $CC( $1 .. $2 ) { for $n ( split /,/, $3 ) { print "$CC$n "} }
turner:~$ perl foo.pl
UA, UI
UA1 UA3 UA4 UA6 UB1 UB3 UB4 UB6 UC1 UC3 UC4 UC6 UD1 UD3 UD4 UD6 UE1 UE3 UE4 UE6
UF1 UF3 UF4 UF6 UG1 UG3 UG4 UG6 UH1 UH3 UH4 UH6 UI1 UI3 UI4 UI6 turner:~$

This is probably because 5.6 expands the whole for(...) list in advance, but 5.8 evaluates it lazily.


Or it could be because the $1 and $2 variables weren't being properly detected as strings by the .. operator in 5.6 unless they had been used in a string context. But they should pretty much always be strings, and I'm not sure I've seen this problem quite like this.

In any case, it's always a little risky to use $1 and friends more than 1 statement after the regex they come from. Too many things clobber 'em at a distance. To be safer, I'd write that as:

$_ = "UA-UI1,3,4,6";
my ($start, $stop, $digits) = m~(..)\-(..)(\d.+)~;
for $CC( $start .. $stop ) { for $n ( split /,/, $digits ) { print "$CC$n "} }


-Ken



Reply via email to