Multiple Arrays


#1

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


#2

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


#3

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


#4

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}")

#5

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!


#6

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


#7

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