Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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







 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with splice in a 2D ARRAY gamo Perl Misc 6 06-17-2013 08:57 PM
Re: Problem with splice in a 2D ARRAY gamo Perl Misc 0 06-17-2013 04:31 PM
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
splice an array into another array agendum97@gmail.com Javascript 1 06-26-2008 12:02 AM
Array function SPLICE doesn't work with Select->OPTIONS array. JavaScript BrianP Javascript 2 09-18-2005 04:20 AM



Advertisments