Gruther
Gruther

Reputation: 93

Hashmap in Perl

I am writing a client API in Perl using FRONTIER::CLIENT module. I am trying to do similar like the following in Perl:

HashMap<Integer, String> message = (HashMap<Integer, String>)client.execute("APIWrapper.login"); 
System.out.println(message.get(1000));

How do I implement the same idea in Perl?

Upvotes: 2

Views: 9557

Answers (2)

Robert S. Barnes
Robert S. Barnes

Reputation: 40578

Hashmaps are a native perl data structure. Any variable declared with the hash symbol % is a hash storing key value pairs. See this documentation on Perl data types. Also see the Perl Data Structures Cookbook.

Edit

See this example

# This can be anything which returns pairs of strings
my %login_message = getData(); # ( 'key1' => 'value1', 'key2' => 'value2' ); 

for my $key ( keys %login_message ) { 
        print "key: $key, value: $login_message{$key}\n"; 
}

sub getData {
        return ( 'key1' => 'value1', 'key2' => 'value2' );
}

Outputs:

key: key2, value: value2
key: key1, value: value1

Upvotes: 8

Zan Lynx
Zan Lynx

Reputation: 54363

The following code is an example of using a hash in Perl:

my %data = (
    red => 1,
    blue => 37,
    green => 99,
);

print $data{'red'}, "\n";

Upvotes: 5

Related Questions