Indeed. The fragment offered can be fixed...
...the named @Cdevices is not required.push (@Mdevices, [$UnitID, $fileno]); for ($k=0; $k<=$#Mdevices; $k++) { print $Mdevices[$k][0],"\n"; } $k=0;
If the more complete code is along the lines:
then @Mdevices is going to end up with 'n' copies of a reference to @Cdevices, which contains a list of the 'n' $UnitID and $fileno seen.my @Mdevices ; my @Cdevices ; while (... something ...) { ... do something to get $UnitID & $fileno ... push (@Cdevices, $UnitID, $fileno); ... do some other stuff ... push (@Mdevices, \@Cdevices); } ;
Whereas the almost identical:
fills @Mdevices with 'n' references to 'n' different annonymous arrays, each containing a $UnitID and $fileno pair.my @Mdevices ; while (... something ...) { my @Cdevices ; ... do something to get $UnitID & $fileno ... push (@Cdevices, $UnitID, $fileno); ... do some other stuff ... push (@Mdevices, \@Cdevices); } ;
Obvious, really.
Alternatively:
will do the trick -- unless the @Cdevices array is required in the loop.my @Mdevices ; while (... something ...) { ... do something to get $UnitID & $fileno ... ... do some other stuff ... push (@Mdevices, [$UnitID, $fileno]); } ;
In reply to Re^2: Multidimensional array
by gone2015
in thread Multidimensional array
by igor1212
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |