Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to access a multi-dimensional array?

Reply
Thread Tools

How to access a multi-dimensional array?

 
 
julianmoors@hotmail.com
Guest
Posts: n/a
 
      09-20-2005
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(0x16cd0fARRAY(0x16cd2aARRAY(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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-20-2005
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(0x16cd0fARRAY(0x16cd2aARRAY(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

 
Reply With Quote
 
 
 
 
julianmoors@hotmail.com
Guest
Posts: n/a
 
      09-20-2005
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!

 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      09-20-2005
<> 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} );
}

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-20-2005
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.
 
Reply With Quote
 
julianmoors@hotmail.com
Guest
Posts: n/a
 
      09-20-2005
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.

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-20-2005
<> 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.
 
Reply With Quote
 
julianmoors@hotmail.com
Guest
Posts: n/a
 
      09-20-2005

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?

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-20-2005
<> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Very annoying error: Access to the path is denied. ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity Jay ASP .Net 2 08-20-2007 07:38 PM
403 Forbidden: You were denied access because: Access denied by access control list Southern Kiwi NZ Computing 6 03-19-2006 05:19 AM
Desktop can't access Laptop but Laptop can access desktop =?Utf-8?B?Qmx1Y2FkZHk3MQ==?= Wireless Networking 2 11-23-2004 01:52 AM
How do I let people access the internet via an access point but not allow them access to my network yar Wireless Networking 4 09-21-2004 03:48 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57