Return a hash from a sub routine

M

matg

I'm sure this is a basic Perl question but I'm a newbie and can't seem
to get my head around it.

I want to pass 3 parameters eg. ("WM","windows","30/12/2005 00:00:00")
to a subroutine that will open a file and return an associative array
to the calling procedure - I believe the issue may lie in the way I
call the sub - any clues? The sub executes correctly and does create
the array but I can't seem to read anything from it once it is returned
(i.e. the foreach loop produces nothing)

Abridged code follows

@Group = "WM";
@Env = "windows";
@Timekey = "30/12/2005 00:00:00";

$Books = &returnBooks(@Group, @Env, @Timekey);

foreach $Book(keys %Books){
print "loop: ".$Book."\n";
}

sub returnBooks{
# Extract parameters
my ($x, $y, $z) = @_;


.......

Open file and create array

if ($BusinessGroup eq $GROUP) {
$BusinessGroups{$Book}=$BusinessGroup;
}

.......

return \%BusinessGroups;
 
J

Joe Smith

matg said:
I'm sure this is a basic Perl question but I'm a newbie and can't seem
to get my head around it.

$Books = &returnBooks(@Group, @Env, @Timekey);

The & is not needed; you're better off without it.
foreach $Book(keys %Books){
print "loop: ".$Book."\n";
}

foreach my $Book (keys %$Books) { ... }

-Joe
 
A

Anno Siegel

matg said:
I'm sure this is a basic Perl question but I'm a newbie and can't seem
to get my head around it.

I want to pass 3 parameters eg. ("WM","windows","30/12/2005 00:00:00")
to a subroutine that will open a file and return an associative array
to the calling procedure - I believe the issue may lie in the way I
call the sub - any clues? The sub executes correctly and does create
the array but I can't seem to read anything from it once it is returned
(i.e. the foreach loop produces nothing)

Abridged code follows

No strict? No warnings?
@Group = "WM";
@Env = "windows";
@Timekey = "30/12/2005 00:00:00";

$Books = &returnBooks(@Group, @Env, @Timekey);

foreach $Book(keys %Books){

The hash %Books has nothing to with the scalar $Books. You must
de-reference the scalar:

foreach my $Book ( keys %$Books ) {
# ...
}

Anno
 
B

Brad Baxter

matg said:
I want to pass 3 parameters eg. ("WM","windows","30/12/2005 00:00:00")
to a subroutine that will open a file and return an associative array
to the calling procedure - I believe the issue may lie in the way I
call the sub - any clues? The sub executes correctly and does create
the array but I can't seem to read anything from it once it is returned
(i.e. the foreach loop produces nothing)

Abridged code follows

"Abridged" is good. "Working" would be better. :)

@Group = "WM";
@Env = "windows";
@Timekey = "30/12/2005 00:00:00";

$Books = &returnBooks(@Group, @Env, @Timekey);

Even though this happens to do what you intended in this case,
this is very much not a good thing to do. It is better to use
scalars when scalars is what you're dealing with, e.g.,

my $Group = "WM";
my $Env = "windows";
my $Timekey = "30/12/2005 00:00:00";

my $Books = returnBooks($Group, $Env, $Timekey);
 
M

matg

Thanks for all the input. Can anyone explain to me why I can't read
diectly from the return as follows:

Instead of :

foreach my $Book(keys %$Books) {
if ($Book eq ($PositionSourceSystem."_".$RiskClass)) {
$GoodBook = 1;
}
}


Why can I not do instead:

if (exists($Books{($PositionSourceSystem."_".$RiskClass)})) {
$GoodBook = 1
}

Clearly this does not work but is there an alternative to the first
example of iterating over every book in the array?
 
P

Paul Lalli

matg said:
Thanks for all the input.

Who are you thanking? For what input? Please quote some context when
posting a reply to Usenet. Keep in mind this is not a forum, a
bulletin board, nor a Google Group.

(Have you read the Posting Guidelines for this newsgroup yet?)
Can anyone explain to me why I can't read
diectly from the return as follows:

Instead of :

foreach my $Book(keys %$Books) {
if ($Book eq ($PositionSourceSystem."_".$RiskClass)) {
$GoodBook = 1;
}
}


Why can I not do instead:

if (exists($Books{($PositionSourceSystem."_".$RiskClass)})) {
$GoodBook = 1
}

Because there's no such variable as %Books. (You are using strict, so
you were told that already by the compiler, right?)

There is, however a variable $Books, which contains a hash reference.
So there's no reason at all that you can't do:

if (exists $Books->{$PositionSourceSystem . '_' . $RiskClass}) {
$GoodBook = 1;
}

Paul Lalli
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top