in reply to Joining and splitting two dimensional arrays

I put together a piece of code that might help you:

#!perl -sw my $input = [[1, 3, 4, 2], [7, 5, 3, 6], [5, 4, 2, 4]]; my $SEP = " "; my $send; sub printarray { my $arr = shift; for my $t (@$arr) { for (@$t) { print "$_ "; } print "\n"; } } sub encode { my $arr = shift; join $SEP x 2, map { join $SEP, @$_ } @$arr; } sub decode { my $in = shift; [(map { [(split $SEP, $_)] } split($SEP x 2, $in))]; } printarray($input); print "\n" x 2; $send = encode($input); print $send . "\n" x 2; $input = decode($send); printarray($input);

It seperates the elements of one line by one separator and two consecutive lines by two separators. Note that although I embedded everything in subs (a nasty little habit of mine) the essential parts are one-liners.
encode expects a reference to an array, decode returns an unnamed array.

Hope this helped.

Replies are listed 'Best First'.
Re: Re: Joining and splitting two dimensional arrays
by bfish (Novice) on Aug 07, 2003 at 18:42 UTC
    Thank you! This looks like it would work and work well, but I have to admit that I already implemented the solution that uses freeze and thaw.