http://www.perlmonks.org?node_id=15668


in reply to hashes

is it not a valid practice to specify the keys you want to print when looping through a hash like that? you seem to be surprised that i would do that, is there a better way? i think i may just forego the foreach altogether.. yes now that i think about it it's not really necessary as the hash will not be varying in size..

Replies are listed 'Best First'.
RE: Re: hashes
by ZZamboni (Curate) on May 31, 2000 at 22:07 UTC
    The thing is, if you know which keys you want to process, and you want them in a specific order, there is no reason to use keys in the first place. Just give the keys in an array, as others have suggested, and index into the hash using them. keys is useful for getting all the keys in a hash, precisely so that you don't have to know their values in advance.

    --ZZamboni

      yeah i realize that now.. keys isn't really useful here.. but hey i wrote this script after maybe 2 or 3 weeks of teaching myself perl and now having had 2 whole months of experience i actually have learned a few things.. thanx :)
RE: Re: hashes
by nuance (Hermit) on May 31, 2000 at 22:09 UTC
    is it not a valid practice to specify the keys you want to print when looping through a hash like that?

    No, unfortunately you can't specify the order that keys will be returned, not using that method anyway. You can either use the keys function and sort its return value, or specify each of the keys in some other way, see posts above for examples

    Nuance

    Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"