I like pc88mxer's solution better than my own, but in the interest in showing different ways of doing the same thing, I'll post it anyway.

use Data::Dumper; my $in = '0 X X X X X X X' . ' X 1 X X X X X X' . ' X X 2 X X X X X' . ' X X X 3 X X X X' . ' X X X X 4 X X X' . ' X X X X X 5 X X' . ' X X X X X X 6 X' . ' X X X X X X X 7'; print Dumper [ string2matrix( $in, 8 ) ]; sub string2matrix { my ( $string, $width ) = @_; my @out; while ( $string ) { my @iter = split /\s+/, $string, $width + 1; $string = scalar @iter > $width ? pop @iter : ''; push @out, \@iter; } return @out; }

In reply to Re: String to two dimensional array by kyle
in thread String to two dimensional array by tashworth

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.