Forum: Ruby Multiple Arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric T. (Guest)
on 2006-05-25 01:02
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
Justin C. (Guest)
on 2006-05-25 10:37
(Received via mailing list)
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
Justin C. (Guest)
on 2006-05-25 10:40
(Received via mailing list)
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
Eric T. (Guest)
on 2006-05-25 17:06
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!
Eric T. (Guest)
on 2006-05-25 22:01
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}")
Eric T. (Guest)
on 2006-05-26 00:41
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
Ezra Z. (Guest)
on 2006-05-26 03:07
(Received via mailing list)
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/.
>


	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
This topic is locked and can not be replied to.