Hey,

I'm having trouble with substr not breaking up strings correctly.

Don't ask why, but I'm having to take a bunch of values, create a big delimited string and then break that string up into 100 character chunks and insert those chunks into a database. I know it's horrible and ugly but I don't have any other options.
There's two systems we're running this on. One is our development webserver and the other is our production server. Everything appears to be working fine on development but twice now on our production webserver we have had failures with one of the chunks being over 100 characters.

The strange thing is that we do a length() check before it inserts and it passes that too. The insert into the database failed of course and I did a character count on the failed chunk. It was 102 characters long. I tried using the same string on our development server and it worked. I also tried running the same code as a command-line perl script and that worked too.

Both webservers are running the same software, which is, albeit, old versions of Apache/Stronghold, mod_perl and perl (5.005_03).

Here's the code that I'm using. @valarray contains the list of fields. @elems will contain the chunks.

my $valstring = join('|', @valarray); my @elems = (); my $len = 100; my $count = 0; do { my $string = substr($valstring, 100*$count, 100); push(@elems, $string); $count++; $len = 100*$count; } while ($len < length $valstring);

I don't know if this being caused by strange characters. Could that be it?

In reply to Strange substr Problem by c0bra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.