Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-20-2012
Registered User
 
Join Date: Jul 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Return a hash table from a sub routine

hello,

i am new to scripting and would like to know how to return a hash table from a sub routine.
i tried the following,

Code:
my %hash_function = ();
hash_function = &return_hash();
sub return_hash
{
    my %hash = ();
      ///populate the hash
    return %hash;
}

but it dosent seem to work. Anything wrong in this??
Sponsored Links
    #2  
Old 12-20-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts
return the reference


Code:
 
$ cat b.pl
#!/usr/bin/perl
use strict;
use warnings;
sub get_hashes();
# Get two variables back
my ($one) = get_hashes();
print "Hash Keys : ";
print "$_ " foreach keys %$one;
print "\n";
sub get_hashes() {
        my %hash1 = (
                "1a" => "b",
                "1b" => "d"
        );
        return (\%hash1);
}
 
$ perl b.pl
Hash Keys : 1b 1a

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Hash Table like implementation in unix anindyabecs UNIX for Dummies Questions & Answers 8 05-25-2011 03:44 PM
Hash table andrew.paul Programming 1 01-07-2011 09:33 AM
nested hash table arthi UNIX for Dummies Questions & Answers 0 06-27-2008 01:56 AM
Creating a Hash Table callmetheskull Programming 4 12-23-2007 02:17 PM
hash table implementations in C Language vlrk Programming 11 07-15-2007 09:33 AM



All times are GMT -4. The time now is 04:05 PM.