Question on watir

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

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

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” [email protected] ۼ:

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 :slight_smile:

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

Try this:

a.sort_by &:first

actually sorting by the keys is the default:

a.sort

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

HI,

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

RAJ

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?

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] }

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?

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

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.

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

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

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

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

Thanks,
RAJ

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

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”

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:

    • upgrade to 1.9.3 or 2.0.0
      (nowadays hashes keep the insertion order)

    • use an array

  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