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

[MacPerl] Dereferencing etc.



Thanks for you responses to:
* Subject: [MacPerl] Foreach, Hash name from <>
* Date: Sat, 13 Feb 1999 15:53:35 +1000

I'm still not 100% clear.

I've done a bit of testing  and the results are as follows:

*********************************
ARRAYS
A. The script
#!perl -w

# Create test array
@test = (1,2,3,4);

# Grab the arrays name
print 'Array to display: ';
$arrayname = <>; (a)
chomp $arrayname;

# Display the array
print 'Following is the contents of the array ',$arrayname,"\n";
foreach $element (@$arrayname) (b) {
print "$element","\n"; (c)
}

B. Test results
(a) string entered (b) string in brackets (c)  printed
     test              @$arrayname             1 nl 2 nl 3 nl 4 nl
     @test             $arrayname              @test
     test              @arrayname              [nothing]
*********************************

*********************************
HASHES
A. The script
#!perl -w

# Create test hash
%test = ('jim','one','jane','two');

# Grab the hashes name
print 'Hash to display: ';
$hashname = <>; (a)
chomp $hashname;

# Display the array
print 'This is the output from %$hashname:',"\n",%$hashname,"\n"; (b)
print 'Following is the contents of the % ',$hashname,"\n";

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

B. Test results
(a) string entered (b) printed         (c) string in brackets
    test               jimonejanetwo       %$hashname
(d)  printed
     key: jim, value: [nothing] nl key: jane, value: [nothing]
*********************************

The %$hashname bit works OK.

The $hashname{$key} bit dosen't. My guess is that the expression
$hashname{$key}
is settup to look for a hash in a table of hashes with the name 'hashname',
or the creation of the hash %test creates a scalar $test implicitly at the
time of it's creation.

Are either of these right?

The references bit has me a little confused. VIncent Nonnenmacher wrote:
"$hashname is a reference to the associative array not the array itself
try to dereference it using the $$ref or $ref-> syntax".

I understand the concept. I can't see how you can distinguish the
occurrence of $hashname in $hashname{$key} from the $hashname in
(keys(%$hashname)).

Is it that the variable name at that location in the expression
$hashname{$key} is a reference by design, so learn that. Or is it some
other context cues that you are seeing?

Thanks In Advance ... John












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