# How to use string as two dimensional array

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--
Fort Worth, Texas

## Re: How to use string as two dimensional array

You cannot use a string as a two dimensional array.

You can however load a 2-D array from a string.

x varies, y is constant.  (and indexes start at zero in Perl)

x varies, y is constant.

x is constant, y varies!

Please exercise more care in describing what you want.

This should get you started:

---------------------
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my \$string = '123456abc';
my @twoD = str2array(3, \$string);
print Dumper \@twoD;

sub str2array {
my(\$dim, \$str) = @_;

die "'\$str' is not evenly divisible by \$dim\n"
if length(\$str) % \$dim;

my @ra;
while ( my \$row = substr \$str, 0, 3, '' ) {
push @ra, [ split //, \$row ];
}
return @ra;
}
---------------------

perldoc perlreftut
perldoc perlref
perldoc perllol
perldoc perldsc

--
Fort Worth, Texas

## Re: How to use string as two dimensional array

Doh!

while ( my \$row = substr \$str, 0, \$dim, '' ) {

--
Fort Worth, Texas

## Re: How to use string as two dimensional array

[...]

Another display of clairvoyance.  I have corrected the erroneous line

As an alternative to substr, the string could be decomposed with
unpack.  That returns the list of all fragments at once, so the
while loop that builds the array can be replaced by map.

sub str2array {
my(\$dim, \$str) = @_;

die "'\$str' is not evenly divisible by \$dim\n"
if length(\$str) % \$dim;

return map [ split //], unpack "(a\$dim)*", \$str;
}

Since we're at it, unpack can do split's job too:

return map [ unpack '(a)*', \$_], unpack "(a\$dim)*", \$str;

concoction.  I like to point out pack/unpack alternatives occasionally,
but I don't mean to recommend them unequivocally.  Pack/unpack sends
everyone on a trip to the docs, so their use must be justified by

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on
"show options" at the top of the article, then click on the

## Re: How to use string as two dimensional array

Anno Siegel wrote:

But your first example has a lower score in golf, so I expect that most
people would stop there.  So, does "split //" run slower than "unpack
'(a)*', \$_" when used on 'a' x 1_000_000?

Benchmark: running split, unpack for at least 3 CPU seconds...
split:  3 wallclock secs ( 3.05 usr +  0.00 sys =  3.05 CPU) @
718665.25/s (n=2191929)
unpack:  1 wallclock secs ( 3.00 usr +  0.00 sys =  3.00 CPU) @
735399.67/s (n=2206199)
Rate  split unpack
split  718665/s     --    -2%
unpack 735400/s     2%     --

Looks like unpack is marginally faster on my AIX server, but I doubt
that it would be noticable to the user.

## Re: How to use string as two dimensional array

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--
Fort Worth, Texas

## Re: How to use string as two dimensional array

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--
Fort Worth, Texas

## Re: How to use string as two dimensional array

I think it's QuickBASIC

## Re: How to use string as two dimensional array

Yes it's Quick Basic 4.5. I use it almost 10 year to solve lot of problems.
Now I try to use Perl, and I have some problem with it.
Thanks to All to try to help me.