AW: returning hashes, and arrays

From: B. Fongo (perl_at_fongo.de)
Date: 03/18/04


To: <beginners@perl.org>
Date: Thu, 18 Mar 2004 02:50:13 +0100


That should work provided you're returning only on list.
It is much better to use reference when passing or retrieving more than
one value.

For instance: Retrieve the values from a sub as refereces.

($xRef, $yRef, $zRef) = example();

#
my @x = @$x;
my @y = @$y;
my $z = $$z;

sub example {
      do this and that;
      # Return 3 values as refereces.
        return (\ @x, \@x, \$z );
        return \(@x, @y, $z);# Simpler
}

HTH
Babs

-----Ursprüngliche Nachricht-----
Von: Stuart White [mailto:poovite@yahoo.com]
Gesendet: Donnerstag, 18. März 2004 02:17
An: beginners@perl.org
Betreff: returning hashes, and arrays

I'm having trouble returning a hash from a subroutine,
and an array from a different subroutine. "Beginning
Perl" said I could return a list, I can't get it to
work. Must the hash and array really be a reference
to the hash and array in order to return them?

Here's my subroutine:
sub ParseLineForHomeAndVisitors()
{
 if ($_
=~/(Spurs|Suns|Mavericks|Lakers|Clippers|Cavaliers|Celtics|Pacers|Piston
s|Wizards|Warriors|Bulls|Hawks|Raptors|Magic|Heat|Kings|Rockets|Nuggets|
Grizzlies|Jazz|Knicks|Nets|Supersonics|'Trail
Blazers'|Bucks|Timberwolves|Hornets|Sixers|Bobcats)/)
 {
  @teams = /([[:alpha:]]+)/g;
 }
 return (@teams);
}

---------
And here's how I'm trying to capture the value:

@teams = ParseLineForHomeAndVisitors();

----
I tried the same thing with my hash:
CreateAbbrevAndNicknamesHash();
and here's the sub:
sub CreateAbbrevAndNicknamesHash()
{
my %AbbrevAndNicknames;
%AbbrevAndNicknames = (
	
IND=>	"Pacers",
	
NJN=>	"Nets",
	
DET	=>	"Pistons",
	
NOH	=>	"Hornets", #check this
	
MIL	=>	"Bucks",
	
CLE	=>	"Cavaliers",
	
BOS	=>	"Celtics",
	
NYK	=>	"Knicks",
	
MIA	=>	"Heat",
	
PHI	=>	"Sixers",
	
TOR	=>	"Raptors",
	
ATL	=>	"Hawks",
	
WAS	=>	"Wizards",
	
ORL	=>	"Magic",
	
CHI	=>	"Bulls",
	
CHA	=>	"Bobcats",
	
SAC	=>	"Kings",
	
MIN	=>	"Timberwolves",
	
SAN	=>	"Spurs",
	
LAL	=>	"Lakers",
	
DAL	=>	"Mavericks",
	
MEM	=>	"Grizzlies",
	
HOU	=>	"Rockets",
	
DEN	=>	"Nuggets",
	
UTA	=>	"Jazz",
	
POR=>	"Trail Blazers",
	
SEA	=>	"Supersonics",
	
LAC	=>	"Clippers",
	
GSW	=>	"Warriors",
	
PHX	=>	"Suns"
	
);
}
-----------
Any suggestions?
__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com
-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... My questions was why the 2nd part of the sub did not change the changes the ... > changes to the array pass it byref. ... > ' new reference ... > ' allocate secondArray and copy its reference ...
    (microsoft.public.dotnet.languages.vb)
  • Questions on Perl Syntax from queue.pm
    ... I don't see an array defined ... When we bless an object, we basically have hash table, correct? ... 87 sub enqueue { ... must be a bug in line 76 because it never returns from a call to dequeue. ...
    (perl.beginners)
  • RE: Array de-referencing problem
    ... But I guess it's better to use an array reference? ... Conversation: Array de-referencing problem ... The values of a hash can only be scalars! ...
    (perl.beginners)
  • Re: Complex Objects in Perl
    ... > sub-object is made up of a number of different objects and an array of an ... Objects in Perl are implemented as a reference to an anonymous hash. ...
    (comp.lang.perl)
  • Re: How to pass function name as a parameter?
    ... > Public Sub operator_index(ByVal index As Integer, ... I'm not sure what you mean by return type of reference, ... want to do is have an object that acts like an array, ... overlook what you can do with Properties, which don't really have a C++ ...
    (microsoft.public.vb.syntax)