Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Receiving hash reference 2

Status
Not open for further replies.

ranadhir

Programmer
Nov 4, 2003
54
IN
I am trynig to receive a hash by reference within my subroutine
sub usefulfacts{
print "Inside usefulfacts\n";
my $localhash=@_;
my %tmphash=%{$localhash};
foreach my $k (keys(%tmphash)){ print "$tmphash($k)"."\n" ;}

}

And it is called as
#now we send in a hash to modify by reference
%gim=(
"environ"=>"challenging",
"strength"=>"mental",
"tough"=>"persistent"
);

foreach my $k (keys(%gim)){ print "$tmphash($k)"."\n" ;}

usefulfacts(\%gim);


The has h is not getting printed inside the subroutine;though it prints successfulyl in the main body.Whats wrong?
Also,if i need to dereference a individual entry in teh hash ,how do i do it?
 
The print statement needs a small tweak. When gathering the values of a hash, you need to use the curly brace around the key ({}).

Code:
{ print "$tmphash[b]{[/b]$k[b]}[/b]"."\n" ;}

- George
 
If you look at the value of $localhash, you will see that it is "1". This is because you are calling an array (@_) in scalar context. When you do this, the count of items in the array is assigned to the variable rather than the actual contents of the array. Try this code for example:
Code:
my @array = qw(a b c);
my $count = @array;
print $count;

Instead of trying to assign the entire array to a scalar, you have to assign a particular position to the scalar. Since their is only one index in @_, you can use the shift command to get the value.
Code:
my $localhash=shift;

FYI, there are some other problems with your code.

This line:
Code:
 foreach my $k (keys(%gim)){ print "$tmphash($k)"."\n" ;}

Should be:
Code:
 foreach my $k (keys(%gim)){ print "$gim($k)"."\n" ;}

It should have $gim{$k} instead of $tmphash. Otherwise, nothing is printed.

Finally, when you call a hash index, you should use braces instead of parentheses.

$gim($k) should be $gim{$k}. Otherwise, your program will print the key surrounded by parentheses, rather than printing the value associated with the key. If you had 'use strict' enabled, you would know about these errors.

Corrected final code
Code:
use strict;
sub usefulfacts{
    print "Inside usefulfacts\n";
    my $localhash= shift;
    my %tmphash = %{$localhash};
    foreach my $k (keys(%tmphash)){ print "$tmphash{$k}"."\n" ;}

}

#now we send in a hash to modify by reference
my %gim = (
    "environ"=>"challenging",
    "strength"=>"mental",
    "tough"=>"persistent"
);

foreach my $k (keys %gim) {
    print "$gim{$k}"."\n";
}

usefulfacts(\%gim);

Just for kicks, try your original code with strict enabled. You will find all kinds of errors that won't even let the program run to show you that nothing is printing inside the sub.
Code:
use strict;
sub usefulfacts{
    print "Inside usefulfacts\n";
    my $localhash=@_;
    my %tmphash=%{$localhash};
    foreach my $k (keys(%tmphash)){ print "$tmphash($k)"."\n" ;}
        
}

#now we send in a hash to modify by reference
%gim=(
    "environ"=>"challenging",
    "strength"=>"mental",
    "tough"=>"persistent"
);

    foreach my $k (keys(%gim)){ print "$tmphash($k)"."\n" ;}

usefulfacts(\%gim);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top