Syntactic Confectionery Delight | |
PerlMonks |
seeking different ways to slice a 2-d arrayby belden (Friar) |
on Apr 25, 2002 at 02:13 UTC ( [id://161836]=perlquestion: print w/replies, xml ) | Need Help?? |
belden has asked for the wisdom of the Perl Monks concerning the following question:
I've got an object method which digs into a hash of arrays of arrays and pulls out the last element of each stored array. Here's my code: After grabbing some water, I walked back and realized that @last_elements is (uh-oh) a transitory variable. (and wouldn'tcha know it, I try to look up on the web why transitory variables are bad, and I can't find anything. Dang; I'm sure dominus wrote about this somewhere. Or maybe this is hard water I'm drinking.) So, with a vague idea that some variables are "good" and others are "bad", I wanted to re-write my method to just return the list of @last_elements without actually storing into a list. (Sidenote: or is this foolish? Am I just inviting bogarts into my code?) After a bit of toying, I realized that there are other ways to iterate over a list than by using for. Enter sort, map, and grep. Prints: for : def def def map : def def def sort: ARRAY(0x80fad5c) ARRAY(0x810c844) ARRAY(0x810c7c0) grep: ARRAY(0x80fad5c) ARRAY(0x810c844) ARRAY(0x810c7c0)
My questions:
blyman
Back to
Seekers of Perl Wisdom
|
|