arraysperlhashmapperl-module

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?


Solution

  • 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