Problem with splice in a 2D ARRAY

G

gamo

Hello, I have a 2D array and want to interchange rows and columns.

I have done this test and it fails talking about flips and flops.
The perldoc -f splice is not of much help to swap 2 rows in a 2D
array (4 parameters as argument).

#!/usr/local/bin/perl -W

for $x (0,1){
for $y (0,1){
$xy[$x][$y]= ++$count;
}
}

splice @xy, 0, 1, $xy[1][0..1];



for $x (0,1){
for $y (0,1){
print "$xy[$x][$y] ";
}
print "\n";
}

__END__

Thanks in advance for any help
 
R

Rainer Weikusat

gamo said:
Hello, I have a 2D array and want to interchange rows and columns.

I have done this test and it fails talking about flips and flops.
The perldoc -f splice is not of much help to swap 2 rows in a 2D
array (4 parameters as argument).

#!/usr/local/bin/perl -W

for $x (0,1){
for $y (0,1){
$xy[$x][$y]= ++$count;
}
}

There is no such thing as 'a 2D array' in Perl. What you create here
is a two-element array containing two references to two anonymous
two-element arrays each containing two numbers each. Swapping the two
'rows' aka 'anonymous two-element arrays' can be done with

@xy[0,1] = @xy[1,0]

or

splice(@xy, 0, 2, @xy[1,0])
 
R

Rainer Weikusat

gamo said:
Hello, I have a 2D array and want to interchange rows and columns.

I have done this test and it fails talking about flips and flops.
The perldoc -f splice is not of much help to swap 2 rows in a 2D
array (4 parameters as argument).

#!/usr/local/bin/perl -W

for $x (0,1){
for $y (0,1){
$xy[$x][$y]= ++$count;
}
}

There is no such thing as 'a 2D array' in Perl. What you create here
is a two-element array containing two references to two anonymous
two-element arrays each containing two numbers. Swapping the two
'rows' aka 'anonymous two-element arrays' can be done with

@xy[0,1] = @xy[1,0]

or

splice(@xy, 0, 2, @xy[1,0])
 
G

gamo

Rainer Weikusat said:
gamo said:
Hello, I have a 2D array and want to interchange rows and columns.

I have done this test and it fails talking about flips and flops.
The perldoc -f splice is not of much help to swap 2 rows in a 2D
array (4 parameters as argument).

#!/usr/local/bin/perl -W

for $x (0,1){
for $y (0,1){
$xy[$x][$y]= ++$count;
}
}

There is no such thing as 'a 2D array' in Perl. What you create here
is a two-element array containing two references to two anonymous
two-element arrays each containing two numbers. Swapping the two
'rows' aka 'anonymous two-element arrays' can be done with

@xy[0,1] = @xy[1,0]

or

splice(@xy, 0, 2, @xy[1,0])

Both works fine, but as I infer there is no simple way to do that
by columns.

thank you very much

--
posted by mobile device



----Android NewsGroup Reader----
http://www.piaohong.tk/newsgroup
 
R

Rainer Weikusat

Ben Morrow said:
Quoth gamo said:
Hello, I have a 2D array and want to interchange rows and columns.

Well, did you try the obvious way?

my @old = ...;
my @new;

for my $r (0..$#old) {
my $row = $old[$r];
for my $c (0..$#$row) {
$new[$c][$r] = $$row[$c];
}
}

I'm not sure it's possible to do that any more elegantly, given that you
can't take a slice vertically through a 2d array.

sub transpose
{
my $in = $_[0];
my (@out, $col);

for $col (0 .. $#{$in->[0]}) {
push(@out, [map { $_->[$col] } @$in[0 .. $#$in]])
}

return \@out;
}

my @a = ([1,2,3,4], [4,5,6,7], [7,8,9,10]);
my $at = transpose(\@a);

for (@$at) {
print("$_ ") for @$_;
print("\n");
}
 
R

Rainer Weikusat

Ben Morrow said:
Quoth Rainer Weikusat said:
Ben Morrow said:
Quoth gamo <[email protected]>:

Hello, I have a 2D array and want to interchange rows and columns.

Well, did you try the obvious way?

my @old = ...;
my @new;

for my $r (0..$#old) {
my $row = $old[$r];
for my $c (0..$#$row) {
$new[$c][$r] = $$row[$c];
}
}

I'm not sure it's possible to do that any more elegantly, given that you
can't take a slice vertically through a 2d array.

sub transpose
{
my $in = $_[0];
my (@out, $col);

for $col (0 .. $#{$in->[0]}) {
push(@out, [map { $_->[$col] } @$in[0 .. $#$in]])
}

Yes, that's the same algorithm, just with map instead of for,

This means it is an equivalent algorithm and specifically,
though it probably is worth pointing out that

map $_->[$col], @2d;

is the way to take a vertical slice.

I wrote it because it came to me that it was actually possible to get
'a vertical slice' out of 'a 2d array' with an expression instead of a
1-by-1 copying loop. It is also possible to get rid of the outer loop
(working with 'arrays' / 'lists' this time for a change):

sub transpose
{
my $col;

return map {
$col = $_;
[map { $_->[$col] } @_]
} 0 .. $#{$_[0]};
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top