Kevin Old wrote:
>
> On Wed, 2003-10-15 at 17:43, James Edward Gray II wrote:
> >
> > On Wednesday, October 15, 2003, at 04:23  PM, Kevin Old wrote:
> >
> > > Hello everyone,
> > >
> > > I have a multidimensional array that I need to split into 4
> > > multidimensional arrays.  I've tried the examples from the Programming
> > > Perl 3rd ed. Chapter 9 for splicing Arrays of Arrays and am not having
> > > any luck.
> > >
> > > Here's an example of how my data looks:
> > >
> > > [1 2 3 4 5 6 7 8 9 10 11 12]
> > > [A B C D E F G H I J  K  L]
> > >
> > > Here's how I need it to look:
> > >
> > > [1 2 3 4] [5 6 7 8]
> > > [A B C D] [E F G H]
> > >
> > > [9 10] [11 12]
> > > [I J] [K  L]
> >
> > Hmm, I'm a little lost at this point.
>
> Yes, you're right.  This is how I meant to define my array:
>
> my @AoA = ( [ 1..12 ], [ 'A'..'L' ] );
>
> Ok, what I need is for the AoA to be reformatted like this into another
> AoA.
>
>
> 1st 4 columns(1-4,A-D), "empty column", Next 4 columns(5-8,E-H)
> "empty row in AoA"
> Next 2 columns(9-10,I-J), "empty column", Last 2 columns(11-12,K-L)
>
> Does this explain it?
>
> Thanks for any help you can offer.
>
> >
> > > The code I used
> > >
> > > #!/usr/bin/perl
> > >
> > > use warnings;
> > > use strict;
> > > use Data::Dumper;
> > >
> > > my @AoA = ();
> > > push @AoA, qw[1 2 3 4 5 6 7 8 9 10 11 12];
> > > push @AoA, qw[A B C D E F G H I J K L];
> >
> > This isn't an array of arrays at this point.  Just want to make sure
> > you know that.  It's one long array with 1 - 12 at the front and A - L
> > at the back.
> >
> > If you meant for this to be a multidimensional array, try:
> >
> > my @AoA = ( [ 1..12 ], [ 'A'..'L' ] );
> >
> > > print Dumper([EMAIL PROTECTED]);
> >
> > This would show that, if it was compiling.
> >
> > > my @newAoA = ();
> > > for (my $startx = my $x = 4; $x <= 8; $x++) {
> > >     for (my $starty = my $y = 7; $y <= 12; $y++) {
> > >             $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
> > >         }
> > > }
> >
> > Obviously this code doesn't work.  It's a referencing issue.  I would
> > try to correct it, but I don't understand the goal. Can you try taking
> > another stab at explaining how the array should look in the end?
>
> BTW, this code is straight from Programming Perl 3rd ed. Chapter 9.

Hi Kevin.

The program below does what I think you mean. But you end up with
a three-dimensional array instead of the two-dimensional one you
started with. Is that right?

HTH,

Rob



use strict;
use warnings;

use Data::Dumper;

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

foreach (4, 4, undef, 2, 2) {

  if ( defined ) {
    push @AoA, [
      [ splice @{$AoA[0]}, 0, $_ ],
      undef,
      [ splice @{$AoA[1]}, 0, $_ ],
    ];
  }
  else {
    push @AoA, undef;
  }
}

splice @AoA, 0, 2;  # Remove the original two rows

print Data::Dumper->Dump( [EMAIL PROTECTED], ['*AoA'] );

** OUTPUT ** (reformatted)

@AoA = (
  [
    [ 1, 2, 3, 4 ],
    undef,
    [ 'A', 'B', 'C', 'D' ]
  ],
  [
    [ 5, 6, 7, 8 ],
    undef,
    [ 'E', 'F', 'G', 'H' ]
  ],
  undef,
  [
    [ 9, 10 ],
    undef,
    [ 'I', 'J' ]
  ],
  [
    [ 11, 12 ],
    undef,
    [ 'K', 'L' ]
  ]
);



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

Reply via email to