Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 14th, 2001, 12:29 AM
Dups Dups is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 30 Dups User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Unhappy

Hello there,
I would like to return an associative array from a subroutine.Could u please suggest how to do it. Also i would like to know the way by which i should access it.

There is one more thing. One of my associative array elements is an array.
Thank u 4 all ur help.

Dups

Reply With Quote
  #2  
Old February 15th, 2001, 11:48 AM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 10
Code:
%hash = sub_name();

sub subname {
    my %return;
#... do whatever here

    return %return;
}


If you want to pass a hash to a sub routine (as well as other values), its best to pass the hash reference to the sub (that is, the memory allocation of the hash). I'll explain:

Code:
%hash_name = (name => 'value', name2 => 'value2');
%returned_hash = sub_hashpass(\%hash_name, <other values>);

sub sub_hashpass {
    my $hash_ref = shift;

    # Change a value of the hash
    $hash_ref->{name} = value;

    return %{$hash_ref};
}


This is just a basic example. You can do all sorts of cool stuff.

Note: if you change a value in $hash_ref [as I did in that sub routine] it will also change in the %hash_name hash. You can change that byref into a byval by point another hash to it's hash values:

my %new_hash = %{$hash_ref};
# Now changing values/keys in %new_has wont affect the original hash at all

I hope I helped.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Associative array-subroutine


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT