How to access a multi-dimensional array?

Discussion in 'Perl Misc' started by julianmoors@hotmail.com, Sep 20, 2005.

  1. Guest

    Here's the code:

    @content =
    (
    [
    "tones" =>
    [
    "Australian Top 10" => ["Australian Chart"],
    "England Euro 2004" => ["Football Chart"],
    "hoops" => ["Ye Old Faithful"],
    "Italia Hits" => ["Italian Chart"],
    "Most Selected Tones" => ["Most Selected"],
    "UK Top10 Chart" => ["UK Chart"]
    ]
    ],
    [
    "polytones" =>
    [
    "Australian Top 10" => ["Australian Chart"],
    "England Euro 2004" => ["Football Chart"],
    "hoops" => ["Ye Old Faithful"],
    "John Williams - Star Wars Them" => ["John Williams"],
    "Most Selected Tones" => ["Most Selected"],
    "UK Top 10 Chart" => ["UK Chart"]
    ]
    ],
    [
    "stereotones" =>
    [
    "Most popular" => ["Most Selected"]
    ]
    ]
    );

    So far I've tried

    foreach (@content) {
    print $_;
    }

    and all I get is ARRAY(0x16cd0f8)ARRAY(0x16cd2a8)ARRAY(0x16cd32c) which
    I can only assume are the references to the arrays. How do I
    dereference them?

    Here's what I want to do using nested for loops:

    1. Go into the content array.

    2. Display the different types of ringtones.

    3. Go into the types.

    4. Display the different charts

    5. Go into the charts.

    6. Display the different headers.
    , Sep 20, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > Here's the code:
    >
    > @content =
    > (
    > [
    > "tones" =>


    This structure is confusing. You're using the "fat arrow" as though
    you're creating a hash reference, but you're actually creating an array
    reference (because you used [ ] instead of { }). There is no
    association between the word 'tones' and the following array reference
    (other than one comes after the other in the array)


    > [
    > "Australian Top 10" => ["Australian Chart"],
    > "England Euro 2004" => ["Football Chart"],
    > "hoops" => ["Ye Old Faithful"],
    > "Italia Hits" => ["Italian Chart"],
    > "Most Selected Tones" => ["Most Selected"],
    > "UK Top10 Chart" => ["UK Chart"]
    > ]
    > ],
    > [
    > "polytones" =>
    > [
    > "Australian Top 10" => ["Australian Chart"],
    > "England Euro 2004" => ["Football Chart"],
    > "hoops" => ["Ye Old Faithful"],
    > "John Williams - Star Wars Them" => ["John Williams"],
    > "Most Selected Tones" => ["Most Selected"],
    > "UK Top 10 Chart" => ["UK Chart"]
    > ]
    > ],
    > [
    > "stereotones" =>
    > [
    > "Most popular" => ["Most Selected"]
    > ]
    > ]
    > );
    >
    > So far I've tried
    >
    > foreach (@content) {
    > print $_;
    > }
    >
    > and all I get is ARRAY(0x16cd0f8)ARRAY(0x16cd2a8)ARRAY(0x16cd32c) which
    > I can only assume are the references to the arrays. How do I
    > dereference them?


    You need to read some documentation.
    perldoc perlreftut
    perldoc perlref
    perldoc perllol
    perldoc perldsc
    are all relevant. I recommend going in that order.

    At its most basic, you dereference a reference by enclosing the
    reference in {} and prepending the sigil of the type you want to
    dereference (@ for arrays, % for hashes, $ for scalars). So the array
    referenced by $_ is @{$_}.

    > Here's what I want to do using nested for loops:
    >
    > 1. Go into the content array.
    >
    > 2. Display the different types of ringtones.
    >
    > 3. Go into the types.
    >
    > 4. Display the different charts
    >
    > 5. Go into the charts.
    >
    > 6. Display the different headers.


    I strongly suggest you re-write your data structure to use hash
    references rather than array references for all your data. Otherwise
    you're going to be stuck with lots of messy array index operations
    (print "$foo[$i]: @{$foo[$i+1]}"; $i+=2;)

    Paul Lalli
    Paul Lalli, Sep 20, 2005
    #2
    1. Advertising

  3. Guest

    To be honest Paul I've only been programming in Perl for a few days,
    but nonetheless I've cracked it.

    @content =
    (
    { type => "tones", chart => "Australian Top 10", header => "Australian
    Chart" },
    { type => "tones", chart => "England Euro 2004", header => "Football
    Chart" },
    { type => "tones", chart => "hoops", header => "Traditional Chart" },
    { type => "tones", chart => "Italia Hits", header => "Italian Hits" },
    { type => "tones", chart => "Most Selected Tones", header => "Most
    Selected" },
    { type => "tones", chart => "UK Top10 Chart", header => "UK Chart" },
    { type => "polytones", chart => "Australian Top 10", header =>
    "Australian Chart" },
    { type => "polytones", chart => "England Euro 2004", header =>
    "Football Chart" },
    { type => "polytones", chart => "hoops", header => "Traditional Chart"
    },
    { type => "polytones", chart => "John Williams - Star Wars Them",
    header => "Star Wars Themes" },
    { type => "polytones", chart => "Most Selected Tones", header => "Most
    Selected" },
    { type => "polytones", chart => "UK Top 10 Chart", header => "UK
    Chart" },
    { type => "stereotones", chart => "Most Popular", header => "Most
    Selected" }
    );

    for ($i = 0; $i < scalar(@content); $i ++) {
    render_table($content[$i]{header}, $content[$i]{type},
    $content[$i]{chart});
    }

    You see I tried using hashes earlier, but I couldn't control the order
    so I used an array of hashes and it worked. God I'm so tired. It's
    03:24 here!
    , Sep 20, 2005
    #3
  4. Dave Weaver Guest

    <> wrote:
    >
    > for ($i = 0; $i < scalar(@content); $i ++) {

    ^^^^^^
    No need for the scalar() here; @content is already in scalar context:
    for ($i = 0; $i < @content; $i ++) {

    > render_table($content[$i]{header}, $content[$i]{type},
    > $content[$i]{chart});
    > }
    >



    FYI, that could be more Perl-ishly written as:

    for my $item ( @content ) {
    render_table( $item->{header}, $item->{type}, $item->{chart} );
    }
    Dave Weaver, Sep 20, 2005
    #4
  5. Anno Siegel Guest

    Dave Weaver <> wrote in comp.lang.perl.misc:
    > <> wrote:
    > >
    > > for ($i = 0; $i < scalar(@content); $i ++) {

    > ^^^^^^
    > No need for the scalar() here; @content is already in scalar context:
    > for ($i = 0; $i < @content; $i ++) {
    >
    > > render_table($content[$i]{header}, $content[$i]{type},
    > > $content[$i]{chart});
    > > }
    > >

    >
    >
    > FYI, that could be more Perl-ishly written as:
    >
    > for my $item ( @content ) {
    > render_table( $item->{header}, $item->{type}, $item->{chart} );
    > }


    Even more compact:

    render_table( @$_{ qw( header type chart)}) for @content;

    but that may be taking it too far beyond readability.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 20, 2005
    #5
  6. Guest

    Thanks for the help guys. Anno, what does @$_ mean? I guess I need to
    learn a lot more about perl before I can call myself a Perl programmer.
    , Sep 20, 2005
    #6
  7. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > Thanks for the help guys. Anno, what does @$_ mean? I guess I need to


    Without context it means nothing. That is true in the large, because
    your posting doesn't show what other posting you are replying to.
    It's also true on a small scale, because "@$_" by itself has no meaning
    (it would be a run-time error).

    For an answer, look up "slice" in perldata.

    > learn a lot more about perl before I can call myself a Perl programmer.


    Please also learn about posting style on Usenet. The posting guidelines
    (posted here regularly) would be a good start.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 20, 2005
    #7
  8. Guest

    Anno Siegel wrote:
    > Dave Weaver <> wrote in comp.lang.perl.misc:
    > > <> wrote:
    > > >
    > > > for ($i = 0; $i < scalar(@content); $i ++) {

    > > ^^^^^^
    > > No need for the scalar() here; @content is already in scalar context:
    > > for ($i = 0; $i < @content; $i ++) {
    > >
    > > > render_table($content[$i]{header}, $content[$i]{type},
    > > > $content[$i]{chart});
    > > > }
    > > >

    > >
    > >
    > > FYI, that could be more Perl-ishly written as:
    > >
    > > for my $item ( @content ) {
    > > render_table( $item->{header}, $item->{type}, $item->{chart} );
    > > }

    >
    > Even more compact:
    >
    > render_table( @$_{ qw( header type chart)}) for @content;
    >
    > but that may be taking it too far beyond readability.
    >
    > Anno
    > --
    > If you want to post a followup via groups.google.com, don't use
    > the broken "Reply" link at the bottom of the article. Click on
    > "show options" at the top of the article, then click on the
    > "Reply" at the bottom of the article headers.


    Thanks for the help guys. Anno, what does @$_ mean? I guess I need to
    learn a lot more about perl before I can call myself a Perl programmer.

    Is that clear enough for you Anno?
    , Sep 20, 2005
    #8
  9. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    >
    > Anno Siegel wrote:
    > > Dave Weaver <> wrote in comp.lang.perl.misc:
    > > > <> wrote:
    > > > >
    > > > > for ($i = 0; $i < scalar(@content); $i ++) {
    > > > ^^^^^^
    > > > No need for the scalar() here; @content is already in scalar context:
    > > > for ($i = 0; $i < @content; $i ++) {
    > > >
    > > > > render_table($content[$i]{header}, $content[$i]{type},
    > > > > $content[$i]{chart});
    > > > > }
    > > > >
    > > >
    > > >
    > > > FYI, that could be more Perl-ishly written as:
    > > >
    > > > for my $item ( @content ) {
    > > > render_table( $item->{header}, $item->{type}, $item->{chart} );
    > > > }

    > >
    > > Even more compact:
    > >
    > > render_table( @$_{ qw( header type chart)}) for @content;
    > >
    > > but that may be taking it too far beyond readability.
    > >
    > > Anno
    > > --
    > > If you want to post a followup via groups.google.com, don't use
    > > the broken "Reply" link at the bottom of the article. Click on
    > > "show options" at the top of the article, then click on the
    > > "Reply" at the bottom of the article headers.

    >
    > Thanks for the help guys. Anno, what does @$_ mean? I guess I need to
    > learn a lot more about perl before I can call myself a Perl programmer.
    >
    > Is that clear enough for you Anno?


    Much better, though you might have trimmed it some (the sig block, in
    particular).

    By itself, "@$_" doesn't have a meaning when $_ is a hashref, as it is
    in the case in point. However "@$_{ 'key1', 'key2', 'key3'}" is a
    hash slice. It retrieves the values corresponding to the keys 'key1',
    'key2' and 'key3' from the hash %$_ and returns a list of these values
    in the given order.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 20, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kk_oop
    Replies:
    1
    Views:
    2,163
  2. Replies:
    7
    Views:
    841
    Ryan Stewart
    Feb 20, 2005
  3. Kevin
    Replies:
    10
    Views:
    1,225
    Chris Uppal
    Sep 15, 2005
  4. Venkat
    Replies:
    4
    Views:
    969
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page