The regex you provide to split specifies a separator, so Perl is splitting your string into chunks separated by sequences of ten characters each. What you get is the following:

$VAR1 = [ <chunk1>, <separator>, <chunk2>, <separator>, <chunk3>, <separator>, <chunk4>, <separator>, <chunk5> ]

And all the chunks are empty except for the last one, which makes sense since there are no constraints on the separators (other than their length): Perl searches for the separator, finds it right at the beginning of the string, and splits it off along with a preceding empty chunk; then the whole process repeats, until you've only got five characters left, not enough to match the separator, so that's your last chunk.

Here's how I'd split a string into ten-character chunks using a regex:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $input = "1234567890abcdefghij0987654321ABCDEFGHIJlmnop"; my @chunks = ($input =~ /.{1,10}/g); print Dumper \@chunks;

This produces:

$VAR1 = [ '1234567890', 'abcdefghij', '0987654321', 'ABCDEFGHIJ', 'lmnop' ];

Note that the quantifier needs to be {1,10} rather than just {10} here to accomodate the final chunk of less than ten characters. The regex engine's greediness will ensure that all chunks but the last will get the full ten characters.


In reply to Re: How to split a string based on character(s) length by AppleFritter
in thread How to split a string based on the length of a sequence of characters within the string by thanos1983

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.