Shiping Wang <[EMAIL PROTECTED]> wrote:

: I try to slice AOA into two subsets, then put them back. The problem
: is when I put them back, I couldn't do what I like, for example:
:
: How can I do it and where I am wrong?


    Let's take the last part first:

: ... and where I am wrong?
[snip]
: my @backcolumn2 = @a;

    Insert:

  print Dumper [EMAIL PROTECTED];

    Since @a is already back together, the following adds it to
itself.

: if (scalar @backcolumn2 != scalar @a) {
:   exit(0);
: } else {
:   for my $i (0 .. $#backcolumn2) {
:   push @{$backcolumn2[$i]}, @{$a[$i]};
:      }
: }
: print "\n";
: sleep(1);
: print "Here again we put another two subset arraies back together\n";
: foreach my $k (@backcolumn2){
:   print join " ",@{$k},"\n";
: }


: How can I do it ... ?

    You could use sub routines to make the program easier to read and
to limit the number of variable names you need.

    First, let's write a sub for printing the AoA. This is a bit
fancier than your version, but the results are similar. The default
column width is 3.

sub dump_aoa {
    my $aoa          = shift;
    my $column_width = shift || 3;

    foreach my $array ( @$aoa ) {
        printf "% ${column_width}s" x @$array, @$array;
        print "\n";
    }
    print "\n\n";
}


    Here's a routine to split the columns. It requires the column
quantity for the left array. It returns two array refs. One for the
left columns and one for the right columns. It needs better error
checking and a default $column to split on.

sub column_split_array {
    my $array_ref   = shift;
    my $column      = shift;

    my( @left, @right );
    foreach my $array ( @$array_ref ) {
        push @left,  [ @$array[ 0       .. $column - 1 ] ];
        push @right, [ @$array[ $column .. $#$array    ]  ];
    }
    return( [EMAIL PROTECTED], [EMAIL PROTECTED] );
}


    Here's one for joining two AoAs. It takes references to two AoAs
and returns a reference to one AoA. It dies on arrays of unequal size,
but doesn't test the size of the inner arrays. It also needs better
error checking.

sub column_join_array {
    my( $left_array, $right_array ) = @_;
    die "Arrays must be same size.\n"
                unless @$left_array == @$right_array;

    my @return_array;
    foreach my $index ( 0 .. $#$left_array ) {
        push @return_array, [
            @{ $left_array->[ $index ]  },
            @{ $right_array->[ $index ] },
        ];
    }
    return [EMAIL PROTECTED];
}


    With these defined your script could be written as:

my @array = (
    [ 'a', 'b', 1, 2 ],
    [ 'c', 'd', 3, 4 ],
    [ 'e', 'f', 5, 6 ],
);

my( $left, $right ) = column_split_array( [EMAIL PROTECTED], 2 );

dump_aoa( $left );
dump_aoa( $right );

dump_aoa( column_join_array( $left, $right ) );

dump_aoa( column_join_array( $left, $left  ) );


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to