From: "James B. Crigler" Date: 2001-05-11T10:40:09+09:00 Subject: [ruby-talk:14967] Array search idiom Here's a question about a ruby idiom. I have an array that contains lines with mammal (classification. animal) pairs like the one found in the code snippet. I need to find the first contiguous list of primates. Assuming the array is called "a", here's how I solved the problem. This does what I want, but is there a better way? The idea is that since the two conditions in the range can be disjoint when it's used as a boolean, I skip lines (i.e., array entries) until I get to the first one that's a primate, then pick up all the lines until there are no more primates, the break out of the iteration. There is one redundant check of the regexp when the first primate is found. a = [ "insectivora. shrews", "insectivora. moles", "insectivora. hedgehogs", "dermoptera. flying lemurs", "chiroptera. bats", "primates. tree shrews", "primates. lemurs", "primates. monkeys", "edentata. sloths", "edentata. armadillos", "edentata. anteaters", "pholidota. scaly anteaters", "primates. marmosets", "primates. great apes", "primates. humans", "lagomorpha. hares", "lagomorpha. rabbits", "lagomorpha. pikas", "rodentia. mice", "rodentia. rats" ] first_group_of_primates = Array.new a.each do |line| next unless line =~ /^primates\./ .. false break if line !~ /^primates\./ first_group_of_primates.push line end -- Jim Crigler | Voice: (770)494-4528 Lockheed Martin Aero | Fax: (770)494-3886 D/73-64 Z/0100 | 86 S. Cobb Dr. | "In 1937, I began, like Lazarus, the impossible return." Marietta GA 30063 | -- Whittaker Chambers