# Looping through 2 arrays

**URL:** <https://www.ruby-forum.com/t/looping-through-2-arrays/96465>\
**Category:** Ruby\
**Created:** [June 28, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/looping-through-2-arrays/96465 "2006-06-28T22:35:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 28, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/looping-through-2-arrays/96465/1 "2006-06-28T22:35:13Z")

</div>

I’m trying to loop through 2 arrays, and notice some odd behaviour,  
probably because the syntax is not correct

unsorted.each do |x|  
sorted.each do |y|  
puts 'this is que\_words ’ + x + ‘.’  
puts 'this is s\_words ’ + y + ‘.’  
end  
end

I added the ‘puts’ in as I’m trying to build a bigger block of code  
and trying to take it one step at a time with some checking in place.  
unsorted has elements, sorted is empty.

When I run the above, nothing is output. When I comment out the  
sorted loop, unsorted outputs it’s elements. Would having an empty  
array effect the output of unsorted or is this just plain wrong ?

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 28, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/looping-through-2-arrays/96465/2 "2006-06-28T22:41:46Z")

</div>

On Jun 28, 2006, at 4:34 PM, Dark A. wrote:

> I added the ‘puts’ in as I’m trying to build a bigger block of code  
> and trying to take it one step at a time with some checking in place.  
> unsorted has elements, sorted is empty.
> 
> When I run the above, nothing is output. When I comment out the  
> sorted loop, unsorted outputs it’s elements. Would having an empty  
> array effect the output of unsorted or is this just plain wrong ?
> 
> Stuart

If sorted is empty it, never enters the sorted.each block, which is  
where all the output is. Try this

unsorted.each do |x|  
puts 'this is que\_words ’ + x + ‘.’  
sorted.each do |y|  
puts 'this is s\_words ’ + y + ‘.’  
end  
end

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [June 28, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/looping-through-2-arrays/96465/3 "2006-06-28T22:48:08Z")

</div>

Okay …thanks! makes sense.  
Apologies also for leaving the que\_words and s\_words in the puts  
statement.  
I meant to change them out since unsorted and sorted are easier to  
understand.

Stuart
