[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Foreach, Hash name from <>



<x-flowed>> Hi all,
>
> This prints "Following is the contents tc.", but not actual hash 
> contents. Why?
>
> Thanks I.A.A. ... John
>
> ######################################### script begin
>
> #!
>
> ##################
> # Script to accept hash name from <STDIN> and display key:value pairs
> ##################
>
> # Grab the hash's name
> print 'Hash to display (no "%" please): ';
> $hashname = <>;
> chomp $hashname;
>
> # Display the key:value pairs
>
> print 'Following is the contents of the hash %',$hashname,"\n";
>
> foreach $hashkey (keys(%$hashname)) {
> print "key: $hashkey, value: $hashname{$hashkey}\n";
> }
>
> *****************************************  script end


$hashname is a reference to the associative array not the array itself
try to dereference it using the $$ref or $ref-> syntax

you should write :

foreach $hashkey (keys %$hashname ) {
print "key: $hashkey, value: $$hashname{$hashkey}\n";

or
foreach $hashkey (keys %$hashname ) {
print "key: $hashkey, value: $hashname->{$hashkey}\n";
---------------------------------------------------------------------
Vincent Nonnenmacher
AgentSpace
116 Av de la Republique
38320 BRESSON
tel :  (33) 476 33 25 06        mailto:Vincent.Nonnenmacher@agentspace.com
fax :  (33) 476 33 25 01        http://www.agentspace.com

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch


</x-flowed>