On Thursday, October 16, 2003, at 09:16 AM, Kevin Old wrote:

James,

On Wed, 2003-10-15 at 22:42, James Edward Gray II wrote:

Now, if you meant a boring two-dimensional table, that's MUCH easier
and more sane.

my @array_2d = ( [ 1.. 4, undef, 5..8 ],
                            [ 'A'..'D', undef, 'E'..'H' ],
                            [ ],
                            [ 9, 10, undef, 11, 12 ],
                            [ 'I', 'J', undef, 'K', 'L' ] );

If it's neither of those, try me again. I'm dumb, but I won't give up.
;)

Yes, this is what I'm trying to get for a result. I'm dumb too, that's why I'm formatting data this way.

What needed to happen was that the data be broken down and displayed
into quadrants out of the two-dimensional array.  I say needed because
the data feed I get has been reformatted so I don't have to worry about
that anymore.

I would however be interested in knowing how to get my previous array
into the @array_2d above.

That's a tricky question for me to answer. I can think of a lot of ways to change it, but I'm not sure I understand the pattern you were using. If I had divided 12 items equally into quadrants it would have been three per quad. I might have done that like:


#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @AoA = ( [ 1..12 ], [ 'A'..'L' ] );

print Dumper([EMAIL PROTECTED]);

# figure out where to break them up
my $quad_count = @{$AoA[0]} / 4;
$quad_count++ if $quad_count =~ s/\.\d+$//;             # round up

my @array_2d = ( [ ], [ ], [ ], [ ], [ ] );
# walk the old rows index by index moving them to the new array
# in the right spot
for my $old_row ( 0, 1 ) {
        for my $index ( 0 .. $#{$AoA[0]} ) {
                my $new_row = $index < 2 * $quad_count ? $old_row : $old_row + 3;
                if ( $index < 2 * $quad_count ) {
                        $array_2d[ $new_row ][ $index ] = $AoA[ $old_row ][ $index ];
                }
                else {
                        $array_2d[ $new_row ][ $index - 2 * $quad_count ] =
                                        $AoA[ $old_row ][ $index ];
                }
        }
}
# add undefs to break up the quads
splice @{ $array_2d[ $_ ] }, $quad_count, 0, undef foreach 0, 1, 3, 4;

print Dumper([EMAIL PROTECTED]);

__END__

However, not understanding your pattern, I probably would have written the much uglier and nearly useless:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @AoA = ( [ 1..12 ], [ 'A'..'L' ] );

print Dumper([EMAIL PROTECTED]);

my @array_2d = ( [ ], [ ], [ ], [ ], [ ] );
push @{ $array_2d[0] }, @{ $AoA[0] }[ 0..3 ], undef, @{ $AoA[0] }[ 4..7 ];
push @{ $array_2d[1] }, @{ $AoA[1] }[ 0..3 ], undef, @{ $AoA[1] }[ 4..7 ];
push @{ $array_2d[3] }, @{ $AoA[0] }[ 8, 9 ], undef, @{ $AoA[0] }[ 10, 11 ];
push @{ $array_2d[4] }, @{ $AoA[1] }[ 8, 9 ], undef, @{ $AoA[1] }[ 10, 11 ];


print Dumper([EMAIL PROTECTED]);

__END__

Given that, I figured it was better just to show it in assignment form.

Maybe it you explain how you were dividing them up, I could do a little better. Sorry if I'm still not helping much.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to