in reply to Strange substr Problem

I found the problem. It's because we had to expand ampersands into the word "and" because of our worthless payment engine, and those transactions had a field with 1 & in them.

*sigh*

Replies are listed 'Best First'.
Re: Re: Strange substr Problem
by BrowserUk (Patriarch) on Mar 14, 2003 at 01:31 UTC

    Glad you found your problem, but a note of caution. If your ever going to upgrade from 5.005 to a newer version of Perl, and if there is any chance that any of your data will contain utf-8 characters, substr might cause you problems in that it deals in characters not bytes.

    You might consider using unpack 'a100' or use bytes to avoid problems as and when you upgrade. A snippet to demonstrate the potential problem and both solutions.

    #! perl -slw use strict; use 5.008; my $utfdata = join'',map{ chr(0xffff + $_) } 1 .. 100; print length $utfdata; #! Gives 100 my @bytes = unpack 'C*', $utfdata; print scalar @bytes; #! Gives 400 my $chunk = substr $utfdata, 0, 100; print length $chunk; #! Gives 100 @bytes = unpack 'C*', $chunk; print scalar @bytes; #! Gives 400 { use bytes; print length $utfdata; #! Gives 400 print length $chunk; #! Gives 400 } my @chunks = unpack '(a100)*', $utfdata; print scalar @chunks; #! Gives 4 print "@{[ map{length} @chunks ]}"; #! Gives 100 100 100 100

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.