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
6 days 20 hours ago
2 weeks 4 days ago
4 weeks 4 hours ago
4 weeks 2 days ago
4 weeks 3 days ago
4 weeks 5 days ago
29 weeks 1 day ago
29 weeks 4 days ago
29 weeks 4 days ago
30 weeks 12 hours ago