Perl: Hashes as reference/function parameter
Following program illustrates how a hash can be passed as a function parameter and also can be referenced: The function is passed hashmap parameter:
%cityHash = (); $cityHash("delhi") = "india"; $cityHash("london") = "uk"; printCityHash(\%cityHash);
In the function, printCityHashwe access the hasmap as follows:
sub printCityHash{ my($hashRef) = @_; print "er"; foreach $city ( keys %$hashRef) { print STDERR "$city: $hashRef->{$city} \n"; }
Thus, the complete program that outputs the hashmap result is:
#!/usr/bin/perl %cityHash = (); $cityHash{"delhi"} = "india"; $cityHash{"london"} = "uk"; printCityHash(\%cityHash); sub printCityHash{ my($hashRef) = @_; print "er"; foreach $city ( keys %$hashRef) { print STDERR "$city: $hashRef->{$city} \n"; } }

Recent comments
24 weeks 13 hours ago
24 weeks 4 days ago
24 weeks 4 days ago
25 weeks 29 min ago
25 weeks 4 days ago
27 weeks 5 days ago
30 weeks 5 days ago
33 weeks 4 days ago
34 weeks 2 days ago
35 weeks 1 day ago