Take choroba's length suggestion to heart; it's probably the best way to go...

But, for purposes of generalizing your modification procedure (which is both overkill and underkill, try this for size:

#!/usr/bin/perl use strict; use warnings; # 927370 - OP seeks to insert 'X' enough times to pad to len=14 AFTER +the LETTERS use 5.012; # NB: First account in this array needs more than one 'X'tension... my @account = qw/CAL2345-06 CALI123456-09 FLOR1234567-01/; for my $account(@account) { my $total_count = 0; while ($account =~ m/./g) { $total_count++ ; } say "At ln 15, |$account| has $total_count characters"; while ($total_count < 14) { substr($account, 4, 0) = 'X'; $total_count++; } continue { # loop till $total_count == 14 ! say "At ln 21, |$account| has $total_count characters"; } next; }

Execution and output:

C:\>927370.pl At ln 15, |CAL2345-06| has 10 characters At ln 21, |CAL2X345-06| has 11 characters <!-- first insert, X in wro +ng position At ln 21, |CAL2XX345-06| has 12 characters At ln 21, |CAL2XXX345-06| has 13 characters At ln 21, |CAL2XXXX345-06| has 14 characters <!-- but we do get to 14, + despite short orig. At ln 15, |CALI123456-09| has 13 characters At ln 21, |CALIX123456-09| has 14 characters <!-- 14 after extension At ln 15, |FLOR1234567-01| has 14 characters <!-- 14 in original array C:\>

hth

Extensively updated: pasted similar output and code but with mismatching ln numbers.


In reply to Re: Counting to add space to a string by ww
in thread Counting to add space to a string 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.