[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Convert a char into numbre

Roberto Decurnex Gorosito

12/18/2007 2:36:00 PM

I am have an String object like "12345678", and an Array object like
[ 8,7,6,5,4,3,2,1].
The main idea is:

myStr = '12345678'
myArray = [8,7,6,5,4,3,2,1]
total = 0

for i in 0..7
total+= myStr[ i ].to_i * myArray[ i ]
end


The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
i can' t get it

Like a quick solution i have to do this:

myStr = '12345678'
myArray = [8,7,6,5,4,3,2,1]
total = 0

for i in 0..7
total += ( String.new << myStr[ i ] ).to_i * myArray[ i ]
end

It's so horrible!!!!! :(

Please, help my code to be beautiful as ruby is

5 Answers

Tim Hunter

12/18/2007 3:01:00 PM

0

Roberto Decurnex Gorosito wrote:
> I am have an String object like "12345678", and an Array object like
> [ 8,7,6,5,4,3,2,1].
> The main idea is:
>
> myStr = '12345678'
> myArray = [8,7,6,5,4,3,2,1]
> total = 0
>
> for i in 0..7
> total+= myStr[ i ].to_i * myArray[ i ]
> end
>
>
> The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
> i can' t get it

Try myStr[i,1]

--
RMagick: http://rmagick.ruby...

yermej

12/18/2007 3:04:00 PM

0

On Dec 18, 8:35 am, Roberto Decurnex Gorosito
<decurnex.robe...@gmail.com> wrote:
> I am have an String object like "12345678", and an Array object like
> [ 8,7,6,5,4,3,2,1].
> The main idea is:
>
> myStr = '12345678'
> myArray = [8,7,6,5,4,3,2,1]
> total = 0
>
> for i in 0..7
> total+= myStr[ i ].to_i * myArray[ i ]
> end
>
> The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
> i can' t get it

Fixnum#chr will give you the String back and then you can use
String#to_i:

total = 0
myArray.each_with_index do |x, i|
total += x * myStr[i].chr.to_i
end
total
=> 120

Alternately, you could just subtract 48 from myStr[i] if you'll always
be using ASCII, but I don't think that's a good way to do things.

As a one-liner:
myStr.split(//).zip(myArray).inject(0) {|acc, arr| acc + arr[0].to_i *
arr[1]}
=> 120

Roberto Decurnex Gorosito

12/18/2007 3:07:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

You are right, thanks!

On Dec 18, 2007 12:01 PM, Tim Hunter <TimHunter@nc.rr.com> wrote:

> Roberto Decurnex Gorosito wrote:
> > I am have an String object like "12345678", and an Array object like
> > [ 8,7,6,5,4,3,2,1].
> > The main idea is:
> >
> > myStr = '12345678'
> > myArray = [8,7,6,5,4,3,2,1]
> > total = 0
> >
> > for i in 0..7
> > total+= myStr[ i ].to_i * myArray[ i ]
> > end
> >
> >
> > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
> > i can' t get it
>
> Try myStr[i,1]
>
> --
> RMagick: http://rmagick.ruby...
>
>

luis lopez

12/18/2007 3:13:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

roberto te llego?
lo que tenes que hacer es , biceps y espalada hoy


2007/12/18, yermej <yermej@gmail.com>:
>
> On Dec 18, 8:35 am, Roberto Decurnex Gorosito
> <decurnex.robe...@gmail.com> wrote:
> > I am have an String object like "12345678", and an Array object like
> > [ 8,7,6,5,4,3,2,1].
> > The main idea is:
> >
> > myStr = '12345678'
> > myArray = [8,7,6,5,4,3,2,1]
> > total = 0
> >
> > for i in 0..7
> > total+= myStr[ i ].to_i * myArray[ i ]
> > end
> >
> > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
> > i can' t get it
>
> Fixnum#chr will give you the String back and then you can use
> String#to_i:
>
> total = 0
> myArray.each_with_index do |x, i|
> total += x * myStr[i].chr.to_i
> end
> total
> => 120
>
> Alternately, you could just subtract 48 from myStr[i] if you'll always
> be using ASCII, but I don't think that's a good way to do things.
>
> As a one-liner:
> myStr.split(//).zip(myArray).inject(0) {|acc, arr| acc + arr[0].to_i *
> arr[1]}
> => 120
>
>

Roberto Decurnex Gorosito

12/18/2007 3:16:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

That's what i was looking for chr, i just forget that one. Thanks!

On Dec 18, 2007 12:05 PM, yermej <yermej@gmail.com> wrote:

> On Dec 18, 8:35 am, Roberto Decurnex Gorosito
> <decurnex.robe...@gmail.com> wrote:
> > I am have an String object like "12345678", and an Array object like
> > [ 8,7,6,5,4,3,2,1].
> > The main idea is:
> >
> > myStr = '12345678'
> > myArray = [8,7,6,5,4,3,2,1]
> > total = 0
> >
> > for i in 0..7
> > total+= myStr[ i ].to_i * myArray[ i ]
> > end
> >
> > The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
> > i can' t get it
>
> Fixnum#chr will give you the String back and then you can use
> String#to_i:
>
> total = 0
> myArray.each_with_index do |x, i|
> total += x * myStr[i].chr.to_i
> end
> total
> => 120
>
> Alternately, you could just subtract 48 from myStr[i] if you'll always
> be using ASCII, but I don't think that's a good way to do things.
>
> As a one-liner:
> myStr.split(//).zip(myArray).inject(0) {|acc, arr| acc + arr[0].to_i *
> arr[1]}
> => 120
>
>