Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How can I cleanly split a string of text into a two-dimensional array

by vroom (Pope)
on Oct 15, 1999 at 02:49 UTC ( #835=perlquestion: print w/ replies, xml ) Need Help??
vroom has asked for the wisdom of the Perl Monks concerning the following question:

I want to take a long string of text and split it into a two-dimensional array of $width x $height. I know of several ways to do this but none of them are as quick and dirty as I'd like them to be? Any suggestions?

Some clarification: I want to read in the data from a bitmap say a PGM file where each 'character' is a pixel. First I'm going to break the header off the string, after that I want to split the data portion into a 2-D array of $widthx$height. I know of ways to do this but am looking for something really quick and clever.
  • Comment on How can I cleanly split a string of text into a two-dimensional array

Replies are listed 'Best First'.
Re: How can I cleanly split a string of text into a two-dimensional array
by chromatic (Archbishop) on Apr 01, 2000 at 10:58 UTC
    How about:
    my $data = "whatever"; my @pixels = [][]; for (my $i = 1; $i < $height; $i++) { (@pixels[$i][1 .. $width], $data) = split('', $data, $width); }
    (untested)
Re: How can I cleanly split a string of text into a two-dimensional array
by fenonn (Chaplain) on Jun 07, 2001 at 23:21 UTC
    Well if there is a char that marks the end of a row (new line for example) you could try something like this.
    @array = map { [split//] } (split/\n/,$string);
Re: How can I cleanly split a string of text into a two-dimensional array
by da w00t (Sexton) on Dec 30, 1999 at 02:24 UTC
    would something liek this work?
    #!/usr/bin/perl use Data::Dumper; open(FILE,"<xpm-pic"); $row=-1;$col=-1; while (<FILE>) { chomp; $row++; $col=-1; foreach $char (split //) { $col++;$pix[$row][$col]=$char; } } print Dumper(\@pix);
Re: How can I cleanly split a string of text into a two-dimensional array
by root (Scribe) on Dec 23, 1999 at 03:56 UTC
    I'm not sure the problem is clear. What do you want each element of an array to be? You could always use nested Foreach loops, of course...
Re: How can I cleanly split a string of text into a two-dimensional array
by BBQ (Deacon) on Dec 24, 1999 at 03:03 UTC
    I agree with root. I guess giving a purpose for the split might be more enlightning. What do you want the two arrays do? What should they control? Are we talking bitmaps here?
Re: How can I cleanly split a string of text into a two-dimensional array
by nothingmuch (Priest) on Jan 14, 2005 at 17:07 UTC
    map { [ split '', $_ ] } unpack join(" ", ("a$width") x $height), $string;
    -nuffin
    zz zZ Z Z #!perl

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2016-07-01 20:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (17 votes). Check out past polls.