develooper Front page | perl.perl5.porters | Postings from December 2003

Re: [perl #24704] Regex replace looses chars

From:
SADAHIRO Tomoyuki
Date:
December 20, 2003 21:16
Subject:
Re: [perl #24704] Regex replace looses chars
Message ID:
20031221141124.7669.BQW10602@nifty.com

> -----------------------------------------------------------------
> [Please enter your report here]
> 
> perl -le '$_="65x"; print; s/65/chr/e; print; print unpack("H*", $_)'
> 65x
> Ax
> 4178
> 
> This is normal.
> 
> perl -le '$_="x65x"; s/x//; print; s/65/chr/e; print; print unpack("H*", $_)'
> 65x
> A
> 4100
> 
> Where did the second x go ????
> 
> [Please do not change anything below this line]

Instead of s/x///, substr($_,0,1,"") causes the same result.
It seems to be related to an offset OK string.

perl -le '$_="x65x"; substr($_,0,1,""); print; s/65/chr/e; print; print unpack("H*", $_)'
65x
A
4100

I tested with various numbers of bytes before and after "65",
where initial value of $_ would be "ab65z", "abc65xyz", and so on.

The offset value is set to $numOfCharsBefore65. In this case,
if $numOfCharsBefore65 >= $numOfCharsAfter65, this problem occurs.

#!perl
for my $numOfCharsBefore65 (1..5) {
    for my $numOfCharsAfter65 (1..5) {
        my $prefix = substr("abcdefghijklm", 0, $numOfCharsBefore65);
        my $suffix = substr("nopqrstuvwxyz", -$numOfCharsAfter65);
        print "$numOfCharsBefore65,$numOfCharsAfter65\t";

        $_ = $prefix.'65'.$suffix;
        substr($_,0,$numOfCharsBefore65,''); # remove $prefix
        print "$_\t"; # should be '65'.$suffix
        s/65/chr/e;
        print "$_\t";
        print unpack("H*", $_), "\n";
    }
}
__END__

1,1	65z	A 	4100
1,2	65yz	Az 	417a00
1,3	65xyz	Ayz 	41797a00
1,4	65wxyz	Axyz 	4178797a00
1,5	65vwxyz	Awxyz 	417778797a00
2,1	65z	Az	417a
2,2	65yz	A z	41007a
2,3	65xyz	Az z	417a007a
2,4	65wxyz	Ayz z	41797a007a
2,5	65vwxyz	Axyz z	4178797a007a
3,1	65z	Az	417a
3,2	65yz	Ayz	41797a
3,3	65xyz	A yz	4100797a
3,4	65wxyz	Az yz	417a00797a
3,5	65vwxyz	Ayz yz	41797a00797a
4,1	65z	Az	417a
4,2	65yz	Ayz	41797a
4,3	65xyz	Axyz	4178797a
4,4	65wxyz	A xyz	410078797a
4,5	65vwxyz	Az xyz	417a0078797a
5,1	65z	Az	417a
5,2	65yz	Ayz	41797a
5,3	65xyz	Axyz	4178797a
5,4	65wxyz	Awxyz	417778797a
5,5	65vwxyz	A wxyz	41007778797a

----
Regards,
SADAHIRO Tomoyuki




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About