Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Convert a char into numbre

Reply
Thread Tools

Convert a char into numbre

 
 
Roberto Decurnex Gorosito
Guest
Posts: n/a
 
      12-18-2007
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

 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      12-18-2007
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.rubyforge.org/

 
Reply With Quote
 
 
 
 
yermej
Guest
Posts: n/a
 
      12-18-2007
On Dec 18, 8:35 am, Roberto Decurnex Gorosito
<(E-Mail Removed)> 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
 
Reply With Quote
 
Roberto Eduardo Decurnex Gorosito
Guest
Posts: n/a
 
      12-18-2007
[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 <(E-Mail Removed)> 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.rubyforge.org/
>
>


 
Reply With Quote
 
luis lopez
Guest
Posts: n/a
 
      12-18-2007
[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 <(E-Mail Removed)>:
>
> On Dec 18, 8:35 am, Roberto Decurnex Gorosito
> <(E-Mail Removed)> 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
>
>


 
Reply With Quote
 
Roberto Eduardo Decurnex Gorosito
Guest
Posts: n/a
 
      12-18-2007
[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 <(E-Mail Removed)> wrote:

> On Dec 18, 8:35 am, Roberto Decurnex Gorosito
> <(E-Mail Removed)> 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
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments