How to access a multi-dimensional array?

J

julianmoors

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.
 
P

Paul Lalli

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
 
J

julianmoors

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!
 
D

Dave Weaver

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} );
}
 
A

Anno Siegel

Dave Weaver said:
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
 
J

julianmoors

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.
 
A

Anno Siegel

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
 
J

julianmoors

Anno said:
Dave Weaver said:
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?
 
A

Anno Siegel

Anno said:
Dave Weaver said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top