Passing a reference as a reference
am 16.08.2007 19:04:48 von pamelamishra
Hi,
I have this question about Perl references.
Suppose i have this %hash which has been populated with key -
values.Subroutine check1 gets the %hash as a refrence from the main
and this refrence has to be passed to another subroutine check2. How
can this be done.Any suggestions would be helpful.
##############################
my %hash;
check1(\%hash);
sub check1
{
my ($hash)=shift;
foreach my $keys (keys $hash)
{
print"$hash{$keys}\n";
}
check2 (I want to send the reference of %hash--which is already a
reference);
}
sub check2
{
my ($hash)=shift;
}
######################
Thanks
Re: Passing a reference as a reference
am 16.08.2007 19:12:39 von Paul Lalli
On Aug 16, 1:04 pm, pamelamis...@gmail.com wrote:
> I have this question about Perl references.
> Suppose i have this %hash which has been populated with key -
> values.Subroutine check1 gets the %hash as a refrence from the main
> and this refrence has to be passed to another subroutine check2. How
> can this be done.Any suggestions would be helpful.
> ##############################
> my %hash;
> check1(\%hash);
>
> sub check1
> {
> my ($hash)=shift;
All fine so far. Only minor problem you have is that you've named
your hash reference $hash. I would call it something like $h_ref or
$hash_ref or something similar.
> foreach my $keys (keys $hash)
Problem here. $hash is not a hash. It's a reference to a hash. If
you want to get at the hash that it references, you have to
dereference the reference:
foreach my $keys (keys %{$hash})
> {
> print"$hash{$keys}\n";
Again. $hash is not a hash. You have to dereference:
print "$hash->{$keys}\n";
> }
> check2 (I want to send the reference of %hash--which is already a
> reference);
No. %hash is the hash that you created in the "main" part of the
script. $hash is a reference to %hash.
There's nothing magical here. Just pass the reference directly:
check2($hash);
>
> }
>
> sub check2
> {
> my ($hash)=shift;
>
> }
Again, though, I rather strongly recommend you choose better names for
your variables. Specifically, name references with some form of
"_ref" on them, to remind you what they actually are.
Paul Lalli
Re: Passing a reference as a reference
am 16.08.2007 23:48:45 von QoS
pamelamishra@gmail.com wrote in message-id: <1187283888.262480.217480@q4g2000prc.googlegroups.com>
>
> Hi,
>
> I have this question about Perl references.
> Suppose i have this %hash which has been populated with key -
> values.Subroutine check1 gets the %hash as a refrence from the main
> and this refrence has to be passed to another subroutine check2. How
> can this be done.Any suggestions would be helpful.
> ##############################
> my %hash;
> check1(\%hash);
>
> sub check1
> {
> my ($hash)=shift;
> foreach my $keys (keys $hash)
> {
> print"$hash{$keys}\n";
> }
> check2 (I want to send the reference of %hash--which is already a
> reference);
> }
>
> sub check2
> {
> my ($hash)=shift;
> }
>
> ######################
> Thanks
If im not mistaken check2($hashRef) should work fine, is there a particular
reason why you want to pass a reference to a reference rather than just pass
the reference?