Velocity Reviews > Perl > Re: Problem with splice in a 2D ARRAY

# Re: Problem with splice in a 2D ARRAY

gamo@telecable.es
Guest
Posts: n/a

 06-17-2013
So you're actually trying to exchange two columns. Yes, that is the way
to do it. For the more general case of reversing the columns of a NxM
matrix, you want

for my \$r (@matrix) {
splice @\$r, 0, \$#r, reverse @\$r;
}

You should look on CPAN: a lot of this sort of basic stuff has already
been implemented.

Ben
---------------------------

very useful, thanks

I see things like Array::Utils, but it's about operations onm two matrix.
Math::MatrixReal is excelent but it's for computations purpose. I don't
know how to search more.

Thanks again

Charles DeRykus
Guest
Posts: n/a

 06-17-2013
On 6/17/2013 1:36 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> for my \$r (@matrix) {
> splice @\$r, 0, \$#r, reverse @\$r;
> }

use List::MoreUtils qw/each_arrayref/;

my @old = (...);
my @new;

my \$iter = each_arrayref( @old );
while (my @row = \$iter->()) { push @new, [reverse @row] };

--
Charles DeRykus

Charles DeRykus
Guest
Posts: n/a

 06-18-2013
On 6/17/2013 3:05 PM, Charles DeRykus wrote:
> On 6/17/2013 1:36 PM, (E-Mail Removed) wrote:
>> for my \$r (@matrix) {
>> splice @\$r, 0, \$#r, reverse @\$r;
>> }

>
>
> use List::MoreUtils qw/each_arrayref/;
>
> my @old = (...);
> my @new;
>

> my \$iter = each_arrayref( @old );
> while (my @row = \$iter->()) { push @new, [reverse @row] };
>

Sorry, I see the mis-attribution now... still not sure what was wanted.
But my above suggestion both transposes and swaps.

If just transposing with no swap:

my \$iter = each_arrayref( @old );
while (my @row = \$iter->()) { push @new, \@row };

--
Charles DeRykus