# Multiple Arrays

**URL:** <https://www.ruby-forum.com/t/multiple-arrays/61550>\
**Category:** Ruby\
**Created:** [May 24, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/multiple-arrays/61550 "2006-05-24T23:02:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuyarico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kuyarico](https://www.ruby-forum.com/u/kuyarico)\
**Post date:** [May 24, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/multiple-arrays/61550/1 "2006-05-24T23:02:49Z")

</div>

As you can probably tell, I’m a noob. I’m working with a program called  
Watir written in ruby. I’m trying to use two txt documents to populate  
two different text fields. I’m able to create the arrays, but having  
trouble populating the text fields properly before Submitting. The first  
field ‘Width’ populates correctly by placing one line at a time. But the  
second places all values in the array into the field. Any help would be  
greatly appreciated.

Here’s what I have:

def size\_entry

```
size_array = IO.readlines("OneOffSizes1.txt")
size_array2 = IO.readlines("OneOffSizes2.txt")

size_array.each_with_index do |size_array,i|

$ie.textField(:name, "width").set("#{size_array}")

$ie.textField(:name, "height").set("#{size_array2}")

$ie.button(:value, "Submit").click

	end

```

end #size\_entry

Thanks,

Eric

---

<div class="post-metadata">

**Author:** ![kuyarico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kuyarico](https://www.ruby-forum.com/u/kuyarico)\
**Post date:** [May 25, 2006, 8:40am UTC](https://www.ruby-forum.com/t/multiple-arrays/61550/2 "2006-05-25T08:40:13Z")

</div>

Justin C. wrote:

> Hi Eric,  
> more descriptive names, too.
> 
> > ```
> > $ie.textField(:name, "height").set("#{size_array2}")
> > 
> > ```
> 
> “height”).set("#{size\_array2[index]}")
> 
> -Justin

Oops - height\_array2 =\> height\_array.

-Justin

---

<div class="post-metadata">

**Author:** ![kuyarico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kuyarico](https://www.ruby-forum.com/u/kuyarico)\
**Post date:** [May 25, 2006, 8:37am UTC](https://www.ruby-forum.com/t/multiple-arrays/61550/3 "2006-05-25T08:37:07Z")

</div>

eric tabora wrote:

> As you can probably tell, I’m a noob. I’m working with a program called  
> Watir written in ruby. I’m trying to use two txt documents to populate  
> two different text fields. I’m able to create the arrays, but having  
> trouble populating the text fields properly before Submitting. The first  
> field ‘Width’ populates correctly by placing one line at a time. But the  
> second places all values in the array into the field. Any help would be  
> greatly appreciated.

Hi Eric,

> Here’s what I have:
> 
> def size\_entry
> 
> size\_array = IO.readlines(“OneOffSizes1.txt”)  
> size\_array2 = IO.readlines(“OneOffSizes2.txt”)

So far so good. (Assuming the arrays are the same size). You might try  
more descriptive names, too.

> size\_array.each\_with\_index do |size\_array,i|

Here is where you start having problems. size\_array.each\_with\_index  
passes in two variables, the /object/ and its /index./ You are using the  
same variable for the object as the array. Try something else like:

```
                  size_array.each_with_index do |width, index|

```

> $ie.textField(:name, “width”).set("#{size\_array}")
> 
> $ie.textField(:name, “height”).set("#{size\_array2}")

The reason why it works for ‘width’ is because you are iterating over  
size\_array on the outside, but on the inside you are using the  
individual values. But for size\_array2 you are using the entire array!  
What I think you are trying to do here is iterate through both arrays in  
parallel.  
Try something like:

```
               $ie.textField(:name, "width").set("#{width}")
               $ie.textField(:name,

```

“height”).set("#{size\_array2[index]}")

> $ie.button(:value, “Submit”).click
> 
> ```
> end
> 
> ```
> 
> end #size\_entry

So the whole thing would look like:

def size\_entry

```
width_array = IO.readlines("OneOffSizes1.txt")
height_array2 = IO.readlines("OneOffSizes2.txt")

width_array.each_with_index do |width, index|

	$ie.textField(:name, "width").set("#{width}")

	$ie.textField(:name, "height").set("#{height_array[index]}")

	$ie.button(:value, "Submit").click
end

```

end

end #size\_entry

Hope that helps.

-Justin

---

<div class="post-metadata">

**Author:** ![kuyarico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kuyarico](https://www.ruby-forum.com/u/kuyarico)\
**Post date:** [May 25, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/multiple-arrays/61550/4 "2006-05-25T20:01:30Z")

</div>

Justin C. wrote:

> Justin C. wrote:
> 
> > Hi Eric,  
> > more descriptive names, too.
> > 
> > > ```
> > > $ie.textField(:name, "height").set("#{size_array2}")
> > > 
> > > ```
> > 
> > “height”).set("#{size\_array2[index]}")
> > 
> > -Justin
> 
> Oops - height\_array2 =\> height\_array.
> 
> -Justin

I think that I’m going to try the route of using one array instead of  
two. My new array looks something like:

size\_array = [6x9, 5x7, 8.5x11]

Is there some way that I can tokenize the array values to populate my  
text fields:

```
$ie.textField(:name, "width").set("#{size_array}")

$ie.textField(:name, "height").set("#{size_array2}")
```

---

<div class="post-metadata">

**Author:** ![kuyarico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kuyarico](https://www.ruby-forum.com/u/kuyarico)\
**Post date:** [May 25, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/multiple-arrays/61550/5 "2006-05-25T15:06:04Z")

</div>

Justin C. wrote:

> Justin C. wrote:
> 
> > Hi Eric,  
> > more descriptive names, too.
> > 
> > > ```
> > > $ie.textField(:name, "height").set("#{size_array2}")
> > > 
> > > ```
> > 
> > “height”).set("#{size\_array2[index]}")
> > 
> > -Justin
> 
> Oops - height\_array2 =\> height\_array.
> 
> -Justin

Thanks Justin, that worked!

---

<div class="post-metadata">

**Author:** ![kuyarico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kuyarico](https://www.ruby-forum.com/u/kuyarico)\
**Post date:** [May 25, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/multiple-arrays/61550/6 "2006-05-25T22:41:20Z")

</div>

eric tabora wrote:

> Justin C. wrote:
> 
> > Justin C. wrote:
> > 
> > > Hi Eric,  
> > > more descriptive names, too.
> > > 
> > > > ```
> > > > $ie.textField(:name, "height").set("#{size_array2}")
> > > > 
> > > > ```
> > > 
> > > “height”).set("#{size\_array2[index]}")
> > > 
> > > -Justin
> > 
> > Oops - height\_array2 =\> height\_array.
> > 
> > -Justin
> 
> I think that I’m going to try the route of using one array instead of  
> two. My new array looks something like:
> 
> size\_array = [6x9, 5x7, 8.5x11]
> 
> Is there some way that I can tokenize the array values to populate my  
> text fields:
> 
> $ie.textField(:name, “width”).set("#{size\_array}")
> 
> $ie.textField(:name, “height”).set("#{size\_array2}")

I answered my own question. How does this look?

def size\_entry

```
size_array = IO.readlines("OneOffSizes.txt")
size_array.each_with_index do |size, index|

size = size.split('x')

	$ie.textField(:name, 

```

“SearchCreativeMaterialsPortletInstance{actionForm.desiredSizeHorizontal}”).set("#{size[0]}")

```
	$ie.textField(:name, 

```

“SearchCreativeMaterialsPortletInstance{actionForm.desiredSizeVertical}”).set("#{size[1]}")

```
	$ie.button(:value, "Search").click
	sleep(1)

	#assert_size
end

```

end #size\_entry

---

<div class="post-metadata">

**Author:** ![kuyarico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e36b37/32.png) [@kuyarico](https://www.ruby-forum.com/u/kuyarico)\
**Post date:** [May 26, 2006, 1:07am UTC](https://www.ruby-forum.com/t/multiple-arrays/61550/7 "2006-05-26T01:07:19Z")

</div>

On May 25, 2006, at 1:41 PM, eric tabora wrote:

> > > >
> > 
> > Is there some way that I can tokenize the array values to populate my
> 
> ## "SearchCreativeMaterialsPortletInstance
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

```
You can also use Array#zip to iterate over two arrays at once:

```

irb(main):012:0\> a = [‘one’, ‘two’, ‘three’]  
=\> [“one”, “two”, “three”]  
irb(main):013:0\> b = [1,2,3]  
=\> [1, 2, 3]  
irb(main):014:0\> a.zip(b).each do |pair|  
irb(main):015:1\* puts pair[0]  
irb(main):016:1\> puts pair[1]  
irb(main):017:1\> end  
one  
1  
two  
2  
three  
3  
=\> [[“one”, 1], [“two”, 2], [“three”, 3]]  
irb(main):018:0\>

-Ezra
