Have you thought of using unpack? Then each time you wish to split the $string into $subStrings you can just change the unpack code list.

I'm not sure how you're representing @list - is each line an element that needs parsing into $var and $noChar or individual elements?

Assuming the structure you have above you could do something like:

my $string = '21090412500011 209 070279501 9047 +0 1111'; my $f = "A6A4A4A15A18A4A1A5A13"; # Dynamically construct from list val +ues. my @subStrings = unpack("A6A4A4A15A18A4A1A5A13",$string);

Then the @subStrings list would hold all the substrings you require.

All you have to do each time you read it is parse the @list and create the $f pack list. Each substring is represented by an "A" (alphanumeric) followed by the length of the substring - i.e. "A6" is a 6 char substring.


In reply to Re: split string by character count by gothic_mallard
in thread split string by character count by SiLiK

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.