in reply to Re: Negative Index with "substr" Not Working
in thread Negative Index with "substr" Not Working

Try regex

sub test { my $str =shift; if(length($str)==11) { $str=~ m/^(\d(\d{6})\d+)$/; print"$1 $2\n"; } else { $str=~ m/^((\d{6})\d+)$/; print"$1$2 $2\n"; } } my $a='01234567890'; my $b ='15556667777'; test($a); test($b);

Output 01234567890 123456
15556667777 555666

Replies are listed 'Best First'.
Re^3: Negative Index with "substr" Not Working
by stevieb (Canon) on Nov 23, 2015 at 23:31 UTC

    Be careful with (ie. don't use) the $a and $b operands... they are special to perl.

    my $x ='01234567890'; my $y ='15556667777'; test($x); test($y);