Marston A. wrote:
I'm trying to do something regarding looping through an array, but only
with elements I need. I'm not sure exactly how to go about it to make it
the most efficient. Is there basically a way to do something like:
for row in @large_row_array WHERE row.element == "something"
... some some stuff
end
What would be the best way to accomplish the above? Would I use perhaps
some nested loops? Thanks for any help, I'm just getting started with
Ruby along with more indepth programming in general and I'm loving it.
I'm not sure if it's always more efficient, but you can use grep in some
cases:
irb(main):003:0> ENV.keys.grep(/ruby/i) {|key| p key}
"RUBYOPT"
"RUBYLIB"
This is more or less the same as
irb(main):004:0> ENV.keys.each {|key| if /ruby/i === key then p key end}
"RUBYOPT"
"RUBYLIB"
(The return values are quite different though.)
So as long as you are working with tests that can be expressed using
#===, you can use #grep. It's compact, and at least sometimes more
efficient.
require 'benchmark'
a = (0..1_000_000).map {|i| i.to_s}
def foo s
end
Benchmark.bmbm do |b|
b.report("grep") do
a.grep(/^10*$/) {|s| foo s}
end
b.report("each-if") do
a.each {|s| if /^10*$/ === s then foo s end }
end
b.report("select-each") do
a.select {|s| /^10*$/ === s}.each {|s| foo s}
end
end
__END__
Rehearsal -----------------------------------------------
grep 0.490000 0.000000 0.490000 ( 0.492667)
each-if 0.640000 0.000000 0.640000 ( 0.638102)
select-each 0.760000 0.000000 0.760000 ( 0.758676)
-------------------------------------- total: 1.890000sec
user system total real
grep 0.490000 0.000000 0.490000 ( 0.487539)
each-if 0.640000 0.000000 0.640000 ( 0.639301)
select-each 0.620000 0.010000 0.630000 ( 0.621152)
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407