Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: String to two dimensional array

by BrowserUk (Pope)
on May 06, 2008 at 22:46 UTC ( #685086=note: print w/ replies, xml ) Need Help??


in reply to String to two dimensional array

sub string2matrix { my( $x, $y, $s ) = @_; die "Can't create $x x $y matrix from string length: ", length $s unless length( $s ) == $x * 2 * $y -1; return map{ map{ [ split ] } $_ } unpack "(A@{[ $x * 2 ]})*", $s; } my $s = '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';; my @a = string2matrix( 8, 8, $s );; pp \@a;; [ [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], ]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: String to two dimensional array
Download Code
Re^2: String to two dimensional array
by tashworth (Initiate) on May 06, 2008 at 22:54 UTC

    Thank you so much for your help. I am new to the perlmonks site, but it looks like a great place to learn perl.

    Anyway, I understand most of what you did except for the line that says:

    pp \@a;;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2015-07-06 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (69 votes), past polls