From: "SHULTZ,BARRY (HP-Israel,ex1)" Date: 2002-02-19T01:19:50+09:00 Subject: RE: hash question Hi, > hello > I read in the pickaxe book that I can also use a Regexp as a hash key > > lets says I have this code: > > hash = Hash.new > hash["bird"] = "vogel" > hash["lynx"] = "luchs" > hash["snake"] = "schlange" > > hash.each {|k,v| > print k, " -> ", v, "\n" if k =~ /^b/ > } > > the output is of course: > bird -> vogel > > > but I like to use the regexp /^b/ before the block > something like > > hash[/^b].each {|k,v| > print k, " -> ", v, "\n" if k =~ /^b/ > } > > but this does not work. I thought that if this would work > it could be faster because the block is only called > for the keys which macht the regexp. > > any ideas how this could be done. You could do this: hash.keys.grep(/^b/){|key| print key, " -> ",hash[key],"\n"} Regards, Barry