Perl: the Markov chain saw PerlMonks

### Splice a multidimensional array

 on Feb 25, 2005 at 03:12 UTC Need Help??

ozgurp has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How can I splice a multidimensional array? Example: Before splicing;
```CR,161508,111847,151085,190251
CR,161509,111847,190251,190252
CR,161510,111847,190252,190150
CR,161511,111847,190150,190153
After splicing second row;
```CR,161508,111847,151085,190251
CR,161510,111847,190252,190150
CR,161511,111847,190150,190153

Replies are listed 'Best First'.
Re: Splice a multidimensional array
by bobf (Monsignor) on Feb 25, 2005 at 04:24 UTC

Splicing an array from an array of arrays (a 2-D array) is no different than splicing an element from an array of scalars (a 1-D array). That's because an AoA is really just an array of array references, and references are stored as scalars. For example:

```use strict;
use warnings;

use Data::Dumper;

my @aoa = ( [ qw( CR 161508 111847 151085 190251 ) ],
[ qw( CR 161509 111847 190251 190252 ) ],
[ qw( CR 161510 111847 190252 190150 ) ],
[ qw( CR 161511 111847 190150 190153 ) ] );

print "orig aoa: @aoa\n";
print Dumper( \@aoa ), "\n";

my @spliced = splice( @aoa, 1, 1 );

print "spliced aoa: @aoa\n";
print Dumper( \@aoa ), "\n";

print "spliced: @spliced\n";
print Dumper( \@spliced ), "\n";

The output from this code is as follows:

```orig aoa: ARRAY(0x183f064) ARRAY(0x18350e8) ARRAY(0x1835190) ARRAY(0x1
+835238)
\$VAR1 = [
[
'CR',
'161508',
'111847',
'151085',
'190251'
],
[
'CR',
'161509',
'111847',
'190251',
'190252'
],
[
'CR',
'161510',
'111847',
'190252',
'190150'
],
[
'CR',
'161511',
'111847',
'190150',
'190153'
]
];

spliced aoa: ARRAY(0x183f064) ARRAY(0x1835190) ARRAY(0x1835238)
\$VAR1 = [
[
'CR',
'161508',
'111847',
'151085',
'190251'
],
[
'CR',
'161510',
'111847',
'190252',
'190150'
],
[
'CR',
'161511',
'111847',
'190150',
'190153'
]
];

spliced: ARRAY(0x18350e8)
\$VAR1 = [
[
'CR',
'161509',
'111847',
'190251',
'190252'
]
];

HTH

Dear monks, Please help me, if I want to remove data from multidimensional array. What if in the above example I just want to remove 1 entity from 2nd dimension. Eg : \$aoa11 = 161509, is to be removed. thus if I try printing \$aoa not it should only give ( CR 111847 190251 190252 )
Re: Splice a multidimensional array
by friedo (Prior) on Feb 25, 2005 at 03:17 UTC
You have a set of comma-delimited data, not a two-dimensional array. If I had to make a completely wild guess based on your example, it appears you want to simply remove an element from the outside array. In that case, splice behaves exactly as you would expect.

```splice @array, 1, 1;

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://434350]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-05-20 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you prefer to work remotely?

Results (73 votes). Check out past polls.

Notices?