On 20.02.2007 17:15, Jim Bob wrote:
> I need to step through a byte array untill I find a certain value, then
> I need to check that the next item in the array is a certain value, it
> is the first index that I require.
> 
> So two questions:
> 
> 1. How would you do it? (my ruby sucks, so interested in seeing decent
> code)
> 
> 2. If your in a block
>      i.e.
>          myarray.each_with_index{|a,i| if(a == 666):return i end}
> then how can you exit the block and return a value without continuing
> through the loop.

You can use break or if inside a method return will work as well.  I.e. 
you could do

# straightforward simple solution
def find_my_index(arr)
   for i in 0 ... arr.length-1
     return i if arr[i] == 66 and arr[i+1] == 55
   end
   # not found
   nil
end

Of course there is a ton of other possible solutions...  I find break 
often not as useful as return because it has the drawback that 
Array#each will always return the array item itself if you do not break 
in between.  That way you have to check the return value of #each which 
I find inelegant.

Kind regards

	robert