in reply to Multidimensional array

Hi,
because you explicitely ask for the first element $Mdevices[$k][0]. You can get proper access to all elements by dereferencing properly:
#!/usr/bin/perl use strict; use warnings; my (@Cdevices,@Mdevices); push (@Cdevices, 'UnitID', 'fileno'); push (@Mdevices, \@Cdevices); # this will show us the actual layout of the array use Data::Dumper; print Dumper(\@Mdevices); # $k=0; $k<=$#Mdevices; $k++ is C-style. Possible, but unnecessary ove +rhead for my $ref (@Mdevices) { print join(' - ', @{$ref}),"\n"; }
prints
$VAR1 = [ [ 'UnitID', 'fileno' ] ]; UnitID - fileno
update: after reading the OP again, it now becomes a bit more clear to me what he actually wants to do and I also found out what the problem is. Look at the following code:
my (@Cdevices,@Mdevices); push (@Cdevices, 'UnitID1', 'fileno1'); push (@Cdevices, 'UnitID2', 'fileno2'); # <- @Cdevices is now qw(UnitI +D1 fileno1 UnitID2 fileno2) push (@Cdevices, 'UnitID3', 'fileno3'); # <- and so on push (@Mdevices, \@Cdevices); # this makes it clear: use Data::Dumper; print Dumper(\@Cdevices); print Dumper(\@Mdevices); for my $ref (@Mdevices) { print $ref->[0],"\n"; }
You want the first element of each sub-array, but there is only one sub-array!!! you would need something like this: push (@Cdevices, ['UnitID3', 'fileno3']); # that will store a reference to an anonymous array
Regards,
svenXY

Replies are listed 'Best First'.
Re^2: Multidimensional array
by gone2015 (Deacon) on Dec 10, 2008 at 17:21 UTC

    Indeed. The fragment offered can be fixed...

    push (@Mdevices, [$UnitID, $fileno]); for ($k=0; $k<=$#Mdevices; $k++) { print $Mdevices[$k][0],"\n"; } $k=0;
    ...the named @Cdevices is not required.

    If the more complete code is along the lines:

    my @Mdevices ; my @Cdevices ; while (... something ...) { ... do something to get $UnitID & $fileno ... push (@Cdevices, $UnitID, $fileno); ... do some other stuff ... push (@Mdevices, \@Cdevices); } ;
    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.

    Whereas the almost identical:

    my @Mdevices ; while (... something ...) { my @Cdevices ; ... do something to get $UnitID & $fileno ... push (@Cdevices, $UnitID, $fileno); ... do some other stuff ... push (@Mdevices, \@Cdevices); } ;
    fills @Mdevices with 'n' references to 'n' different annonymous arrays, each containing a $UnitID and $fileno pair.

    Obvious, really.

    Alternatively:

    my @Mdevices ; while (... something ...) { ... do something to get $UnitID & $fileno ... ... do some other stuff ... push (@Mdevices, [$UnitID, $fileno]); } ;
    will do the trick -- unless the @Cdevices array is required in the loop.

      Hello!

      This code really did the trick!

      my @Mdevices ; while (... something ...) { ... do something to get $UnitID & $fileno ... ... do some other stuff ... push (@Mdevices, [$UnitID, $fileno]); } ;

      Thanks to all of you for help

      Igor