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

In reply to Re: Multidimensional array by svenXY
in thread Multidimensional array by igor1212

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.