For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2006 > Print dereferenced hash of arrays keys and values









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 Print dereferenced hash of arrays keys and values
Derek Basch

2006-05-24, 7:02 pm

Why do I get a syntax error on this one:

for my $value_resp ( keys %$reactivate_account_hash_ref ) {
print "$value_resp: @{ %$reactivate_account_hash_ref{$value_res
p}
}\n";
}

Yes, I read the perldocs....No, I still don't get it.

Thanks,
Derek Basch

usenet@DavidFilmer.com

2006-05-24, 7:02 pm

Derek Basch wrote:
> Why do I get a syntax error on this one:


> for my $value_resp ( keys %$reactivate_account_hash_ref ) {
> print "$value_resp: @{ %$reactivate_account_hash_ref{$value_res
p} }\n";
> }


Because %$whatever isn't Perl.

> Yes, I read the perldocs....No, I still don't get it.


That's good. I also recommend:

http://www.augustmail.com/~tadmc/cl...guidelines.html

--
http://DavidFilmer.com

Jim Gibson

2006-05-24, 7:02 pm

In article <1148515953.043326.238050@j73g2000cwa.googlegroups.com>,
Derek Basch <dbasch@yahoo.com> wrote:

> Why do I get a syntax error on this one:
>
> for my $value_resp ( keys %$reactivate_account_hash_ref ) {
> print "$value_resp: @{ %$reactivate_account_hash_ref{$value_res
p}
> }\n";
> }
>
> Yes, I read the perldocs....No, I still don't get it.


Because you are trying to access an element of a hash but you are not
using the '$' character to signify a scalar value. The '%' character
refers to the entire hash. Instead of

%$reactivate_account_hash_ref{$value_res
p}

you want

$$reactivate_account_hash_ref{$value_res
p}

or perhaps clearer would be

${$reactivate_account_hash_ref}{$value_r
esp}

or also

$reactivate_account_hash_ref->{$value_resp}

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Matt Garrish

2006-05-24, 10:01 pm


"Derek Basch" <dbasch@yahoo.com> wrote in message
news:1148515953.043326.238050@j73g2000cwa.googlegroups.com...
> Why do I get a syntax error on this one:
>
> for my $value_resp ( keys %$reactivate_account_hash_ref ) {


Keys are not values. You should really try and name your variables better.

> print "$value_resp: @{ %$reactivate_account_hash_ref{$value_res
p}


So you normally access the values of a hash by prepending a '%'? You
obviously haven't looked very hard at the docs.

Think $href->{$key} and then go back to perlref.

Matt


Tad McClellan

2006-05-24, 10:01 pm

Derek Basch <dbasch@yahoo.com> wrote:
> Why do I get a syntax error on this one:
>
> for my $value_resp ( keys %$reactivate_account_hash_ref ) {
> print "$value_resp: @{ %$reactivate_account_hash_ref{$value_res
p}
> }\n";
> }



Because you have not dereferenced the reference:

print "$value_resp: @{ $reactivate_account_hash_ref->{$value_resp} }\n";
or
print "$value_resp: @{ ${$reactivate_account_hash_ref}{$value_r
esp} }\n";


> Yes, I read the perldocs....



You have read over 2000 pages? Wow!

You only need to read the _right_ 8 pages, namely:

perldoc perlreftut


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Tad McClellan

2006-05-25, 4:01 am

usenet@DavidFilmer.com <usenet@DavidFilmer.com> wrote:
> Derek Basch wrote:
>
>
> Because %$whatever isn't Perl.



Yes it is.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Derek Basch

2006-05-25, 7:02 pm

Thanks everyone,

Darn, I thought that $$ is what I needed to do. I come from the python
world and keeping my scalars straight is a new way of thinking for me.

However, you Perl guys crack me up. You all seem to love fighting with
each other......ALOT!

Thanks,
Derek Basch

DJ Stunks

2006-05-25, 7:02 pm


Derek Basch wrote:
> you Perl guys crack me up. You all seem to love fighting with
> each other......ALOT!


so's your momma!

:-)~

-jp

Priit Randla

2006-05-29, 4:07 am

Derek Basch wrote:
> Why do I get a syntax error on this one:
>
> for my $value_resp ( keys %$reactivate_account_hash_ref ) {
> print "$value_resp: @{ %$reactivate_account_hash_ref{$value_res
p} }\n";
> }
>

Because %$reactivate_account_hash_ref{$value_res
p} isn't an array
reference. Try $reactivate_account_hash_ref->{$value_resp} instead.

> Yes, I read the perldocs....No, I still don't get it.
>
> Thanks,
> Derek Basch
>

Priit
Sponsored Links







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

Copyright 2008 codecomments.com