in reply to Shifting of 2D array slices

It looks like you want something like this:

$ perl -le' use Data::Dumper; my @bigarray = ( [ "123*jeff", "tortoise", "qwerty" ], [ "456*john", "parrot", "azerty" ], [ "789*jane", "budgie", "abcdef" ], ); print Dumper \@bigarray; @bigarray = map [ map split( /\*/ ), @$_ ], @bigarray; print Dumper \@bigarray; ' $VAR1 = [ [ '123*jeff', 'tortoise', 'qwerty' ], [ '456*john', 'parrot', 'azerty' ], [ '789*jane', 'budgie', 'abcdef' ] ]; $VAR1 = [ [ '123', 'jeff', 'tortoise', 'qwerty' ], [ '456', 'john', 'parrot', 'azerty' ], [ '789', 'jane', 'budgie', 'abcdef' ] ];

Replies are listed 'Best First'.
Re^2: Shifting of 2D array slices
by holli (Abbot) on Dec 07, 2008 at 00:57 UTC
    @bigarray = map [ map split( /\*/ ), @$_ ], @bigarray;
    The second map is not neccessary. The return value of map can be a list, so
    @bigarray = map [ split /\*/ ], @bigarray;
    will do fine, too.


    holli, /regexed monk/

      split, by default, operates on $_, but the elements of @bigarray are references. You need to explicitly dereference them and then split the elements. That's why the internal map is necessary. Your suggestion leaves @bigarray looking this way:

      $VAR1 = [ [ 'ARRAY(0x504290)' ], [ 'ARRAY(0x531f00)' ], [ 'ARRAY(0x531e60)' ] ];
Re^2: Shifting of 2D array slices
by AnomalousMonk (Archbishop) on Dec 07, 2008 at 06:16 UTC
    @bigarray = map [ map split( /\*/ ), @$_ ], @bigarray;
    The second map statement (map split(/\*/), @$_) in the reply of jwkrahn is not necessary. In addition, it has the side effect of splitting all elements (strings) of the referenced array even though the intent seems to be to split only the first. None of the other elements contains the particular split pattern being used, so the extraneous splits have no effect; however, this approach depends on the assumption that the split pattern never appears in the other elements, and no one knows what the future may hold.

    (Note that the test data in @bigarray are slightly different.)

    >perl -wMstrict -le "use Data::Dumper; my @bigarray = ( [ '123*jeff', 'tortoise', 'qwe*rty', ], [ '456*john', 'parrot', 'aze*rty', ], [ '789*jane', 'budgie', 'abc*def', ], ); @bigarray = map [ map split(/\*/), @$_ ], @bigarray ; print Dumper \@bigarray; " $VAR1 = [ [ '123', 'jeff', 'tortoise', 'qwe', 'rty' ], [ '456', 'john', 'parrot', 'aze', 'rty' ], [ '789', 'jane', 'budgie', 'abc', 'def' ] ]; >perl -wMstrict -le "use Data::Dumper; my @bigarray = ( [ '123*jeff', 'tortoise', 'qwe*rty', ], [ '456*john', 'parrot', 'aze*rty', ], [ '789*jane', 'budgie', 'abc*def', ], ); @bigarray = map [ split(/\*/, $_->[0]), @$_[ 1 .. $#$_ ] ], @bigarray ; print Dumper \@bigarray; " $VAR1 = [ [ '123', 'jeff', 'tortoise', 'qwe*rty' ], [ '456', 'john', 'parrot', 'aze*rty' ], [ '789', 'jane', 'budgie', 'abc*def' ] ];
    Actually, I would prefer an approach using a for loop such as that of davidrw. In particular, the approach using splice can easily be modified to split any arbitrary element; this can also be done with the map approach, but with less concision.