Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by vroom (His Eminence)
on Oct 15, 1999 at 02:49 UTC ( [id://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 BBQ (Curate) 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
Re: How can I cleanly split a string of text into a two-dimensional array
by Anonymous Monk 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...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-11-10 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    chatterbot is...






    Results (38 votes). Check out past polls.