[Purdue-pm] Can do better with map somehow?

Dave jacoby jacoby at purdue.edu
Wed Nov 24 10:57:02 PST 2010


So, I have a function that dumps a DB into a hashref. Assume the dump 
looks like a long version of this.

$VAR1 = {
	'a' => {
		'foo' => 1 ,
		'bar' => 'Cheers' ,
		} ,
	'b' => {
		'foo' => 3 ,
		'bar' => 'Crow' ,
		} ,
	'c' => {
		'foo' => 1 ,
		'bar' => 'Neon Cactus' ,
		} ,
	...
	}

I would love to have that same hashref, except only containing only 
parts where 'foo' == 1. I can certainly loop it, but I'm learning more 
and more about map and it's finding its way into much more of my code.

my $second ; %$second = map { $_ => $first->{$_} } keys %$first ; gets 
me a full copy, but I can't imagine a way to get grep to work for me and 
sort can just organize 'em. Is there a cool thing I'm missing?

-- 
Dave Jacoby                         Address: WSLR S049
Code Maker                          Mail:    jacoby at purdue.edu
Purdue University                   Phone:   765.49.67368


More information about the Purdue-pm mailing list