Problem with splice in a 2D ARRAY

Discussion in 'Perl Misc' started by gamo, Jun 17, 2013.

  1. gamo

    gamo Guest

    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
     
    gamo, Jun 17, 2013
    #1
    1. Advertising

  2. gamo <> writes:
    > 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])
     
    Rainer Weikusat, Jun 17, 2013
    #2
    1. Advertising

  3. gamo <> writes:
    > 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])
     
    Rainer Weikusat, Jun 17, 2013
    #3
  4. gamo

    gamo Guest

    Rainer Weikusat <> Wrote in message:
    > gamo <> writes:
    >> 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
     
    gamo, Jun 17, 2013
    #4
  5. Ben Morrow <> writes:
    > Quoth gamo <>:
    >>
    >> 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");
    }
     
    Rainer Weikusat, Jun 17, 2013
    #5
  6. Rainer Weikusat <> writes:

    [...]

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


    This is, of course, equivalent to

    push(@out, [map { $_->[$col] } @$in])
     
    Rainer Weikusat, Jun 17, 2013
    #6
  7. Ben Morrow <> writes:
    > Quoth Rainer Weikusat <>:
    >> Ben Morrow <> writes:
    >> > Quoth gamo <>:
    >> >>
    >> >> 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]};
    }
     
    Rainer Weikusat, Jun 17, 2013
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter

    splice or split an array

    Peter, Sep 7, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    113
    Anno Siegel
    Sep 8, 2004
  2. BrianP
    Replies:
    2
    Views:
    290
  3. Replies:
    1
    Views:
    143
    Richard Cornford
    Jun 26, 2008
  4. gamo
    Replies:
    0
    Views:
    155
  5. Replies:
    2
    Views:
    166
    Charles DeRykus
    Jun 18, 2013
Loading...

Share This Page