----- Original Message -----
From: "Ken Tozier" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, May 31, 2003 5:24 PM
Subject: Returning arrays from subroutines... how?
> I'm sure this is an easy one but after Googling for hours, I still
> don't get it. Given the following subroutine, how do I return the
> result array? Nothing I try works.
>
> sub GetMarmots
> {
> @result = ();
> $result{'steppe marmot '} = 4;
> $result{'himalayan marmot'} = 3;
> $result{'mongolian marmot'} = 1;
> $result{'woodchuck'} = 6;
>
> return @ result;
> }
>
> Thanks in advance,
>
> Ken
>
Hi Ken -
1. you declare an array, but treat it like a hash reference.
2. If you want to return a hash, tryp something like:
# you shoulg use these 2 uses - they save a lot of heartache!
use strict;
use warnings;
sub GetMarmots
{
my %result = ();
$result{'steppe marmot '} = 4;
$result{'himalayan marmot'} = 3;
$result{'mongolian marmot'} = 1;
$result{'woodchuck'} = 6;
return %result;
}
#you then would call the subroutine and get the
#hash by calling is as in:
my %stuff = GetMarmots ();
print "$_ => $stuff{$_}\n" for (sort keys %stuff);
Aloha => Beau;
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]