First off, it would really help if you could provide an SSCCE.

With the code provided it is far from clear what you are trying to do, or where the error is occurring.

My first question would be: at what line of the provided code is the error actually occurring?

My second question would be: is length($opposite{$side1}) always 1, or is it sometimes 0, or more than 1?

My third question would be: does your real code have use strict and use warnings enabled?

Then I would observe that the code would be easier to read if you used //x on your regexp, and if you had my $length = $end - $start + 1 and used that throughout. It would also be useful to add more instrumentation: show $type in each iteration, show the full string before and after each change, show all the parameters to substr() before each invocation.

I suspect that with better instrumentation the answer will be obvious to you, but if you want help from us you need to help us to help you. And that starts with a Short, Self-Contained, Correct Example.

Hugo


In reply to Re: substr out of str error - but why? by hv
in thread substr out of str error - but why? by Anonymous Monk

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.