Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: String to two dimensional array

by kyle (Abbot)
on May 07, 2008 at 02:37 UTC ( #685125=note: print w/replies, xml ) Need Help??


in reply to String to two dimensional array

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://685125]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-09-25 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?