# Question on watir

**URL:** <https://www.ruby-forum.com/t/question-on-watir/224437>\
**Category:** Ruby\
**Created:** [November 12, 2012, 5:45am UTC](https://www.ruby-forum.com/t/question-on-watir/224437 "2012-11-12T05:45:39Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 12:59pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/81 "2013-03-13T12:59:26Z")

</div>

Thank you very much this would help me,

and I have another question,

consider the below hash,

a={1=\>2,3=\>4,5=\>6}

when I try to print the key values, it is not printing in the order

a.each do |key,value|  
puts key  
end

it’s printing 5 3 1

Is there any way could I able to print in order.?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:06pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/82 "2013-03-13T13:06:22Z")

</div>

hi,

[:text\_fields, :select\_lists, :checkboxes].each do |type|  
$browser.send(type).each do |i|  
puts i, type.to\_s[0…-2]  
end  
end

This code stuns me. Beautifully written.Please consider my last question  
about Hash.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:09pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/83 "2013-03-13T13:09:24Z")

</div>

As far as I know, there is no “order” in hash. If the order matters, you  
should use list instead of hash.  
2013. 3. 13. 9:00 “Raj pal” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ۼ:

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:08pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/84 "2013-03-13T13:08:08Z")

</div>

Raj pal wrote in post #1101398:

> hi,
> 
> [:text\_fields, :select\_lists, :checkboxes].each do |type|  
> $browser.send(type).each do |i|  
> puts i, type.to\_s[0..-2]  
> end  
> end
> 
> This code stuns me. Beautifully written.Please consider my last question  
> about Hash.
> 
> RAJ

Hah, doesn’t feel like it to me! Thanks anyway 🙂

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:17pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/85 "2013-03-13T13:17:50Z")

</div>

a.sort\_by &:first

Here a is what? Is it hash?

RAJ  
I don’t understand how would I write the full code?

a=a.sort\_by &:first

Is it like that?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:10pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/86 "2013-03-13T13:10:55Z")

</div>

Try this:

a.sort\_by &:first

actually sorting by the keys is the default:

a.sort

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:20pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/87 "2013-03-13T13:20:28Z")

</div>

Ok ok I understood here, But I don’t want the sorted order, But I want  
the exact same order for an example,

a={‘a’=\>‘raj’,‘z’=‘gopal’,‘b’=‘hello’}

If I want to store or print the keys in an array it has to be in the  
same order

Is it possible?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:41pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/88 "2013-03-13T13:41:04Z")

</div>

HI,

Still it is printing in the same order it doesn’t provide the result in  
right order.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:47pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/89 "2013-03-13T13:47:11Z")

</div>

Are you trapping the order before you mess with the hash?  
If the order is important, why are you using a hash?  
Can a sorting algorithm duplicate the order?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 1:22pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/90 "2013-03-13T13:22:52Z")

</div>

If you know the input order then use your inputs to retreive the values  
in the same order later:

a={‘a’=\>‘raj’,‘z’=‘gopal’,‘b’=‘hello’}  
order = a.keys

#Change the values and mess up the order…

#Output in the same order  
order.each { |k| puts k, a[k] }

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:16pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/91 "2013-03-13T14:16:33Z")

</div>

Well, that’s a lot of logic to cover with a quick Q&A.  
Have you considered interfacing with the excel sheet directly rather  
than using a hash?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:04pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/92 "2013-03-13T14:04:03Z")

</div>

I am using excel sheet to store the values, while I am reading those  
values I am storing inside the hash, first row act as a key, remaining  
column act as a values, Now I need the order of those column to match  
with the fields, But i don’t have.

## If i had the Order then I would not have to write like

begin  
$browser.text\_field(:id,’’).set(contact[‘Name’]  
rescue =\>e  
puts e  
end

begin  
$browser.text\_field(:id,’’).set(contact[‘age’]  
rescue =\>e  
puts e  
end

## begin $browser.text\_field(:id,’’).set(contact[‘Gender’] rescue =\>e puts e end

consider the above three code, I can take id dynamically and I can pass  
into text\_field(:id,’ ') here and at the same time if i can get the  
Name,age,Gender in the order then I can pass that too so I can put into  
the loop, I no need to write the static code for thousands of field. But  
unfortunately it’s not working.

Thanks for your help.  
RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:28pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/93 "2013-03-13T14:28:51Z")

</div>

In that case you may as well use excel’s columns and rows to iterate,  
and update it live with the results. No more worrying about disorganised  
hashes that way.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:18pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/94 "2013-03-13T14:18:27Z")

</div>

Yes,

begin  
$browser.text\_field(:id,’’).set(contact[‘Gender’]  
rescue =\>e  
puts e  
end

In the above line I am interacting with excel to retrieve this value  
contact[‘Gender’]

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:33pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/95 "2013-03-13T14:33:03Z")

</div>

How can you iterate excel column and row,

If i retrieve the first row, headings will be the keys and values would  
be values, If i retrieve the second rows then same headings will be the  
keys and second row values would be the values. And my long project  
incorporate this view.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:36pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/96 "2013-03-13T14:36:33Z")

</div>

How you iterate depends on what you’re using to access excel. There’s  
always the option of reading the whole lot into an array, modifying it  
in memory during your browser interaction, and then rewriting all at  
once

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:55pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/97 "2013-03-13T14:55:03Z")

</div>

Thank you for your help today, you have taught a lot today to me.

Thanks,  
RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 2:40pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/98 "2013-03-13T14:40:51Z")

</div>

Yes that we have already done, from that I was reading my HASH. OK i  
will look into it if that is possible by array,so that order would not  
change.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 4:21pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/99 "2013-03-13T16:21:51Z")

</div>

If you really want a hash you could incorporate the cell addresses:  
For example:

hash = { ‘Name’ =\> { B1: ‘James’, B2: ‘Tiberius’, B3: ‘Kirk’ },  
‘Age’ =\> { C2: 1, C3: 2, C4: 3 },  
‘Gender’ =\> { D2: ‘Man’, D3: ‘Cat’, D4: ‘God’ }  
}

> hash[‘Name’][:B3]  
> =\> “Kirk”

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [March 13, 2013, 5:03pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/100 "2013-03-13T17:03:51Z")

</div>

Am 13.03.2013 12:59, schrieb Raj pal:

> a.each do |key,value|  
> puts key  
> end
> 
> it’s printing 5 3 1

then you are using a very old Ruby version.

> Is there any way could I able to print in order.?

1. In case you need insertion order:

2. In case you need sorting order: sort before printing

1.8.7 :005 \> a.sort.each do |key,value|  
1.8.7 :006 \> puts key  
1.8.7 :007?\> end  
1  
3  
5

[Previous page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=4)

[Next page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=6)
