For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > March 2004 > Associative Arrays









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Associative Arrays
Hale

2004-03-18, 6:47 pm

I have the following:

my $var;

$var->{VALUE-SET1}->{subset1}
$var->{VALUE-SET1}->{subset2}
$var->{VALUE-SET1}->{subset3}

how can I loop through $var->{VALUE-SET1} and get the individual keys /
values for subsetx?

Thanks


Dave Cross

2004-03-18, 6:47 pm

On Sun, 29 Feb 2004 04:42:17 +0000, Hale wrote:

> I have the following:
>
> my $var;
>
> $var->{VALUE-SET1}->{subset1}
> $var->{VALUE-SET1}->{subset2}
> $var->{VALUE-SET1}->{subset3}
>
> how can I loop through $var->{VALUE-SET1} and get the individual keys /
> values for subsetx?


foreach (keys %{$var->{VALUE-SET1}}) {
print "$_ - $var->{VALUE-SET1}->{$_}\n";
}

Dave...


Hale

2004-03-18, 6:47 pm

thank you for the prompt reply.

Question: What exactly do the brackets mean? In other words, what is the
point of {$var-> ... } ?


"Dave Cross" <dave@dave.org.uk> wrote in message
news:pan.2004.02.29.08.40.39.142567@dave.org.uk...
> On Sun, 29 Feb 2004 04:42:17 +0000, Hale wrote:
>
>
> foreach (keys %{$var->{VALUE-SET1}}) {
> print "$_ - $var->{VALUE-SET1}->{$_}\n";
> }
>
> Dave...
>
>



Matt Garrish

2004-03-18, 6:47 pm


"Hale" <paulj1999@yahoo.com> wrote in message
news:m7p0c.1642$Zo6.94@twister.socal.rr.com...
> "Dave Cross" <dave@dave.org.uk> wrote in message
> news:pan.2004.02.29.08.40.39.142567@dave.org.uk...
/[color=darkred]
> thank you for the prompt reply.
>
> Question: What exactly do the brackets mean? In other words, what is the
> point of {$var-> ... } ?
>


Please don't top-post your follow-ups. As to your question, it can be
answered in one word: clarity. It's actually %{ } and it should make it
absolutely clear to anyone reading the code that what is inside of those
braces, in this case $var->{VALUE-SET1}, is a reference to a hash. Braces
can be used around any variable name in Perl, but generally aren't needed (I
use them most often when I'm interpolating variables into a double-quoted
string and want/need to make it clear what is the variable and what is the
text).

Matt


Dave Cross

2004-03-18, 6:47 pm

On Sun, 29 Feb 2004 12:34:57 -0500, Matt Garrish wrote:

> "Hale" <paulj1999@yahoo.com> wrote in message


> As to your question, it can be
> answered in one word: clarity. It's actually %{ } and it should make it
> absolutely clear to anyone reading the code that what is inside of those
> braces, in this case $var->{VALUE-SET1}, is a reference to a hash.
> Braces can be used around any variable name in Perl, but generally
> aren't needed (I use them most often when I'm interpolating variables
> into a double-quoted string and want/need to make it clear what is the
> variable and what is the text).


Actually, that's not quite true. In this case the braces aren't just
there for clarity. The brackets are essential. Try running the code
without them.

Dave...

Matt Garrish

2004-03-18, 6:47 pm


"Dave Cross" <dave@dave.org.uk> wrote in message
news:pan.2004.03.01.07.04.35.809774@dave.org.uk...
> On Sun, 29 Feb 2004 12:34:57 -0500, Matt Garrish wrote:
>
>
>
> Actually, that's not quite true. In this case the braces aren't just
> there for clarity. The brackets are essential. Try running the code
> without them.
>


Clear to the reader; clear to the perl interpreter. I guess I didn't stress
the latter enough in my response. My bad...

Matt


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com