trouble with passing reference of hash.

S

sam

Hi,

I m trying to pass the reference of a hash variable to subroutine's
subroutine, but I found no record being passed to the function. Here is
the actual code:

#!/usr/bin/perl

sub print_hashes
{
local ($hash) = @_;

pass_one_more_level(\%hash);
#foreach my $name ( keys(%hash) ) {
# print "$name: ", join( "--", %{$hash{$name}} ), "\n";
#}
}

sub pass_one_more_level
{
local ($hash) = @_;

foreach my $name ( keys(%hash) ) {
print "$name: ", join( "--", %{$hash{$name}} ), "\n";
}
}

sub main {
local %hash1 = ('sales_subtotal'=>100);
local %hash2 = ('sales_subtotal'=>150);
local %hash3 = ('sales_subtotal'=>500);

local %myhash_A = (heading1 => {sales_subtotal => 10},
heading2 => {sales_subtotal => 15},
heading3 => {sales_subtotal => 50});

local %myhash_B = (heading1 => %hash1,
heading2 => %hash2,
heading3 => %hash3);

print_hashes(\%myhash_A);
print_hashes(\%myhash_B);

}

main();

What is the correct way for fixing this error?

Thanks
Sam
 
S

Sherm Pendley

sam said:
What is the correct way for fixing this error?

You can't just make sh*t up and hope it works. Read the docs for the
correct way to pass arguments to subroutines:

perldoc perlsub

And stop using local - it's not doing what you think it does.

Have you read the posting guidelines that appear here frequently?

sherm--
 
S

sam

Bernard El-Hagin wrote:

Please *please* read about local(). You don't seem to understand what
it's for.
Thanks. I have changed all local to my(), and get rid of the strict and
warings. Now the code look like as follow:

#!/usr/bin/perl

sub print_hashes
{
my ($hash) = @_;

#pass_one_more_level(\%hash);
foreach my $name ( keys(%hash) ) {
print "$name: ", join( "--", %{$hash{$name}} ), "\n";
}
}

sub pass_one_more_level
{
my ($hash) = @_;

foreach my $name ( keys(%hash) ) {
print "$name: ", join( "--", %{$hash{$name}} ), "\n";
}
}

sub main {

my %hash1 = ('sales_subtotal'=>100);
my %hash2 = ('sales_subtotal'=>150);
my %hash3 = ('sales_subtotal'=>500);

my %myhash_A = (heading1 => {sales_subtotal => 10},
heading2 => {sales_subtotal => 15},
heading3 => {sales_subtotal => 50});

my %myhash_B = (heading1 => \%hash1,
heading2 => \%hash2,
heading3 => \%hash3);

print_hashes(\%myhash_A);
print_hashes(\%myhash_B);

}

main();


Thanks
Sam
 
J

Josef Moellers

sam said:
Bernard El-Hagin wrote:


Thanks. I have changed all local to my(), and get rid of the strict and
warings.

Don't get rid of "use strict" and "use warnings", they are there to help
you:

Global symbol "%hash" requires explicit package name at XxX.pl line 10.
Global symbol "%hash" requires explicit package name at XxX.pl line 11.
Global symbol "%hash" requires explicit package name at XxX.pl line 19.
Global symbol "%hash" requires explicit package name at XxX.pl line 20.
Execution of XxX.pl aborted due to compilation errors.
#!/usr/bin/perl

sub print_hashes
{
my ($hash) = @_;

#pass_one_more_level(\%hash);
foreach my $name ( keys(%hash) ) {

What makes you think that when you have a reference it will magically
turn into a hash? If you have a reference, you must dereference it first!
print "$name: ", join( "--", %{$hash{$name}} ), "\n";

and here
}
}

sub pass_one_more_level
{
my ($hash) = @_;

foreach my $name ( keys(%hash) ) {

and here
print "$name: ", join( "--", %{$hash{$name}} ), "\n";

and here

Try to read up on references and see how they work.
 
A

Anno Siegel

sam said:
Hi,

I m trying to pass the reference of a hash variable to subroutine's
subroutine, but I found no record being passed to the function. Here is
the actual code:

#!/usr/bin/perl

No warning, no strict.
sub print_hashes
{
local ($hash) = @_;

Don't use local() and package variables unless there's a reason.
pass_one_more_level(\%hash);

You are using %hash here, but you never assigned anything to it, only
to $hash. The two variables have nothing to do with another.
#foreach my $name ( keys(%hash) ) {
# print "$name: ", join( "--", %{$hash{$name}} ), "\n";
#}
}

sub pass_one_more_level
{
local ($hash) = @_;

foreach my $name ( keys(%hash) ) {

Same problem as above. You assign to $hash, but you use %hash.
print "$name: ", join( "--", %{$hash{$name}} ), "\n";
}
}

What is the sub pass_one_more_level() good for? It has no discernible
function, it only makes your code harder to follow. Try to get a one-
level approach right first, then you can go on and nest it, if you have
to.
sub main {

You don't need a sub "main" in Perl. Just put the statements you want
executed in the main file.
local %hash1 = ('sales_subtotal'=>100);
local %hash2 = ('sales_subtotal'=>150);
local %hash3 = ('sales_subtotal'=>500);

local %myhash_A = (heading1 => {sales_subtotal => 10},
heading2 => {sales_subtotal => 15},
heading3 => {sales_subtotal => 50});

local %myhash_B = (heading1 => %hash1,
heading2 => %hash2,
heading3 => %hash3);

Under "warnings" Perl would have told you there's something wrong here.
print_hashes(\%myhash_A);
print_hashes(\%myhash_B);

}

main();

What is the correct way for fixing this error?

There's more than one error in your code. In particular, you seem to
be unclear about how and when referencing and de-referencing are used.
Read perlreftut and perldsc for some background on that.

Otherwise, use lexical variables (declared with "my") where possible.
In Perl, "local" is not a variable declaration, only a runtime action.

This code does what you want, without any extra nesting.

my %myhash = (
heading1 => {sales_subtotal => 10},
heading2 => {sales_subtotal => 15},
heading3 => {sales_subtotal => 50},
);

print_hashes( \ %myhash);

sub print_hashes {
my $hash = shift;
print "$_: ", join( '--', %{ $hash->{ $_}}), "\n" for keys %$hash;
}

Anno
 
X

xhoster

sam said:
Hi,

I m trying to pass the reference of a hash variable to subroutine's
subroutine, but I found no record being passed to the function. Here is
the actual code:

use strict or die;

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top