Home >> perldatatypes >>Assigning values to hash (or associative arrays/lookup tables)%us_states=
("California","Sacramento","Wisconsin","Madison","New York","Albany");
We can also use => operator to identify the key to the left, and the value to the right; if the => operator encounters bare words in key positions, they will be automatically quoted (note "New York", however, which consists of two words and MUST be quoted %us_states= (California=>"Sacramento",Wisconsin=>"Madison","New York"=>"Albany"); In above example California is key and Sacromento is value. Similarily Wisconsin is key and Madison is value. Printing: print "Capital of California is " . $us_states{"California"} . "\n\n";
printing all values(using for loop): #!/usr/bin/perl
%states=
(California=>"Sacramento",Wisconsin=>"Madison","New York"=>"Albany");
foreach my $keys(keys %us_states)
{
print "KEY:$keys VALUE:$us_states{$keys}\n";
}
output
KEY:Wisconsin VALUE:Madison
KEY:New York VALUE:Albany
KEY:California VALUE:Sacramento
Note
Hashes of hashesexample...%companies_products = (
google => {
email => "gmail",
},
microsoft => {
email => "hotmail",
database => "MSsql server",
"wordprocessing" => "ms office",
},
yahoo => {
email => "yahoo mail",
chat => "yahoo messenger",
},
);Perl functions
|
Perl @ARGV %ENV Perl Arrays Perl Data Types Perl list Perl references Perl Scalar data types Perl Special Variables
|