I think what the OP is trying to do is to assemble the string once, in a single pass. In his added info, jeanluca, he said the assembling code is "very complex!".

I think eric256 in Re: Create string with a delay and Zaxo in Re: Create string with a delay are both on the right track, but eric256's "len($a . $b . $c . $d . $x)" represents a separate pass through the complexity. For Zaxo's solution, the second argument of the sprintf needs to be "length($str)+10", calculated just before $x is added.

Combining and extending the two approaches a little, I'd suggests

$str = $a . $b . $c .$d; $xpos_spot = length( $str ); $e = '0000000000'; $xpos_len = length($e); $str .= $e; $str .= $f; ... $str .= $w; substr $str, $xpos_spot, $xpos_len, sprintf('%${xpos_len}d',length($str)); $str .= $x; ...
Note that this automatically adjusts for the length of the "fill-in" zeroes, if we change that part in a later revision, usually a good idea. Also the code still works if we insert a "$str .= $aplus", somewhere in the complexities of assembling the first part of the string.

There's a way to set up a kind of pointer into the part of the string you want to modify later, one that you can assign to. It uses a reference to "substr" with pre-defined parameters (known as currying). See BrowserUK's excellent Re: lhs substr(): refs vs. scalars writeup.


In reply to Re: Create string with a delay by rodion
in thread Create string with a delay by jeanluca

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.