#!/usr/bin/perl -w ####################################################### # class3_02_hash_reverse # # see how reverse works with a hash: making keys # into values, and vice versa! # # modified by Sharon Tuttle from "Learning Perl", # by Schwartz and Phoenix # # last modified: 4-13-03 ####################################################### %order_totals = ("chocolate", 13, "hamburger", 10, "tofu dog", 12); %totals_ordered = reverse %order_totals; print "\$totals_ordered{13}: $totals_ordered{13}\n"; print "\$totals_ordered{10}: $totals_ordered{10}\n"; print "\$totals_ordered{12}: $totals_ordered{12}\n"; # what if a VALUE is in the hash twice? When reversed, the key for # the "later" will overwrite the key for the "former"; $order_totals{"shake"} = 12; print "\nNow 12 is a value for two different keys...\n"; print " BUT when reversed, key 12 will get only 1 value:\n\n"; %totals_ordered = reverse %order_totals; print "\$totals_ordered{13}: $totals_ordered{13}\n"; print "\$totals_ordered{10}: $totals_ordered{10}\n"; print "\$totals_ordered{12}: $totals_ordered{12}\n"; # end of class3_02_hash_reverse