Multiply values in an array?

7 views
Skip to first unread message

stuck

unread,
May 9, 2008, 1:39:44 PM5/9/08
to Watir General
Hi All,

I am having trouble trying to multiply values in an array with a
number. for eg. I have an array like this:

excelAppRange= ["0.1456","0.2345","0.9090","0.7890"]

What I need to do is take each element in the array and multiply it by
100.

I tried the following code but it doesnt work:

excelAppRange.length.times do |y|

excelVal = excelAppRange[y].to_s

excelValString = excelVal.to_s

multiplyVal = (excelValString * 100)

end

I tried converting the string to "to_f " but when I use the "to_f" , I
get a no method error. So I am not sure how I can take that string
and , multiply it by 100. Please suggest what else I could try.

Thanks in advance.

Ravi

unread,
May 9, 2008, 1:51:05 PM5/9/08
to Watir General
This may help:

excelAppRange= ["0.1456","0.2345","0.9090","0.7890"];
new_array=[];
excelAppRange.each {|i| new_array << i.to_f*100};
p new_array;

Mark Anderson

unread,
May 9, 2008, 1:56:58 PM5/9/08
to watir-...@googlegroups.com
It would be helpful if I knew what you wanted your output to look like and
where you wanted the output to be.

My first thought was:
excelAppRange.map! {|n| n.to_f*100}
which leaves excelAppRange as an array of results

My next thought was that you might need numerical strings for some reason:
excelAppRange.map! {|n| (n.to_f*100).to_s}

Finally, you might not want to change excelAppRange, so you would want:
new_mapped_array = excelAppRange.map {|n| (n.to_f*100).to_s}

Hope this helps,
/\/\ark


Hi All,

excelAppRange.length.times do |y|

excelValString = excelVal.to_s

end

Thanks in advance.

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 3089 (20080509) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 3089 (20080509) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


stuck

unread,
May 9, 2008, 2:24:12 PM5/9/08
to Watir General
Hi Thanks for all the help but when I tried the code
excelAppRange.map! {|n| (n.to_f*100).to_s}

I get the error : NoMethodError: undefined method `to_f' for
[0.0494125]:Array

On May 9, 10:56 am, "Mark Anderson" <mander...@drillinginfo.com>
wrote:

Mark Anderson

unread,
May 9, 2008, 2:40:43 PM5/9/08
to watir-...@googlegroups.com
I don't think your data is in the format that you say it is in. I can
reproduce your error if I set:
excelAppRange = [[0.14],[0.23],[0.90],[0.79]]

This means you don't need to convert to floats, but you do need to unwrap
another level of array. I'm still not sure how you want your output, as an
array of arrays:
excelAppRange.map! {|n| [n[0]*100]}
or as an array of strings:
excelAppRange.map! {|n| (n[0]*100).to_s}

If this doesn't work for you, please show us the actual data structures you
are converting from and the actual resultant data structure you expect.

stuck

unread,
May 9, 2008, 3:13:47 PM5/9/08
to Watir General
Hi Mark,

Sorry to bother you soo many times but my brain is just blocked n I
have no clue what I am doing wrong.

Ok so here is exactly what I have to do...I have an excel that has
rows of data like 0.4556% 0.5644% etc.

So what I do is I read the excel sheet and put its values in an array,
and then need to compare these excel values with my UI ( which are in
the form 4.556 in one cell, the other one has 5.644 etc)

My code now does this:

excelAppRange = @worksheet.Range("b5:b8").Value

excelAppRange.length.times do |y|

puts excelAppRange[y]

excelAppRange.map! {|n|[n[y]*100]}

end

abs =$ie.cell(:id,cellId[x].to_s).text.delete('%') # This gets
values in the cell Ids in the UI

Now this is giving me an error : NoMethodError: undefined method `*'
for nil:NilClass

Also once I have the array with the right values I just plan to do an

assert_equal(excelAppRange[y])to_s),sprintf("%.2f",abs).to_s)

Once again , I thank you again for all you time .


On May 9, 11:40 am, "Mark Anderson" <mander...@drillinginfo.com>

Mark Anderson

unread,
May 9, 2008, 3:54:33 PM5/9/08
to watir-...@googlegroups.com
I think that this will take care of the error that you are seeing:

excelAppRange = @worksheet.Range("b5:b8").Value

excelAppRange.map! {|n| n[0]*100}

abs =$ie.cell(:id,cellId[x].to_s).text.delete('%')

I'm not sure if it will pass your assert or not, but perhaps it will get you
close enough.


assert_equal(excelAppRange[y])to_s),sprintf("%.2f",abs).to_s)

/\/\ark

stuck

unread,
May 9, 2008, 8:05:31 PM5/9/08
to Watir General
Hey Mark ...that Worked! Thanks a tonn..u saved my day!

On May 9, 12:54 pm, "Mark Anderson" <mander...@drillinginfo.com>
Reply all
Reply to author
Forward
0 new messages