Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #6985 > unrolled thread

Convert MatchData to Integer

Started byqoexcel@gmail.com
First post2014-06-02 13:07 -0700
Last post2014-06-03 22:53 +0200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  Convert MatchData to Integer qoexcel@gmail.com - 2014-06-02 13:07 -0700
    Re: Convert MatchData to Integer Robert Klemme <shortcutter@googlemail.com> - 2014-06-02 22:11 +0200
      Re: Convert MatchData to Integer qoexcel@gmail.com - 2014-06-03 03:22 -0700
        Re: Convert MatchData to Integer Michael Uplawski <michael.uplawski@uplawski.eu> - 2014-06-03 19:51 +0200
        Re: Convert MatchData to Integer Robert Klemme <shortcutter@googlemail.com> - 2014-06-03 22:53 +0200

#6985 — Convert MatchData to Integer

Fromqoexcel@gmail.com
Date2014-06-02 13:07 -0700
SubjectConvert MatchData to Integer
Message-ID<6d4b5bb3-7b43-4222-8924-504b04f1bd6c@googlegroups.com>
Hi Everyone,i want to get integer from string, something like this:      
           
           <Loading DOM finished: 41 ms>  

using command            /\d+/.match(self.loadDOMstat)          
            
, but actually i don't have integer, i have MatchData .  
How can i convert MatchData to integer in Ruby?Moreover you can give me a hint, how to get number  from this string..  
Thank you in advance!!!    

[toc] | [next] | [standalone]


#6986

FromRobert Klemme <shortcutter@googlemail.com>
Date2014-06-02 22:11 +0200
Message-ID<bv443qF687bU1@mid.individual.net>
In reply to#6985
On 06/02/2014 10:07 PM, qoexcel@gmail.com wrote:
> Hi Everyone,i want to get integer from string, something like this:
>
>             <Loading DOM finished: 41 ms>
>
> using command            /\d+/.match(self.loadDOMstat)
>
> , but actually i don't have integer, i have MatchData .
> How can i convert MatchData to integer in Ruby?Moreover you can give me a hint, how to get number  from this string..

Generally you use method Integer() to convert String into an int:

irb(main):001:0> s = "123"
=> "123"
irb(main):002:0> n = Integer(s)
=> 123
irb(main):003:0> n.class
=> Fixnum

In your case you can do this:

irb(main):004:0> s = '<Loading DOM finished: 41 ms>'
=> "<Loading DOM finished: 41 ms>"
irb(main):005:0> n = s[/<Loading DOM finished: (\d+) ms/, 1]
=> "41"
irb(main):006:0> n = Integer(n)
=> 41

Cheers

	robert

[toc] | [prev] | [next] | [standalone]


#6987

Fromqoexcel@gmail.com
Date2014-06-03 03:22 -0700
Message-ID<b826794f-0b44-4dfc-bbb7-c929284f133d@googlegroups.com>
In reply to#6986
вторник, 3 июня 2014 г., 0:11:38 UTC+4 пользователь Robert Klemme написал:
> On 06/02/2014 10:07 PM, qoexcel@gmail.com wrote:
> 
> > Hi Everyone,i want to get integer from string, something like this:
> 
> >
> 
> >             <Loading DOM finished: 41 ms>
> 
> >
> 
> > using command            /\d+/.match(self.loadDOMstat)
> 
> >
> 
> > , but actually i don't have integer, i have MatchData .
> 
> > How can i convert MatchData to integer in Ruby?Moreover you can give me a hint, how to get number  from this string..
> 
> 
> 
> Generally you use method Integer() to convert String into an int:
> 
> 
> 
> irb(main):001:0> s = "123"
> 
> => "123"
> 
> irb(main):002:0> n = Integer(s)
> 
> => 123
> 
> irb(main):003:0> n.class
> 
> => Fixnum
> 
> 
> 
> In your case you can do this:
> 
> 
> 
> irb(main):004:0> s = '<Loading DOM finished: 41 ms>'
> 
> => "<Loading DOM finished: 41 ms>"
> 
> irb(main):005:0> n = s[/<Loading DOM finished: (\d+) ms/, 1]
> 
> => "41"
> 
> irb(main):006:0> n = Integer(n)
> 
> => 41
> 
> 
> 
> Cheers
> 
> 
> 
> 	robert

Thank you very much, Robert!! Yes, now i have number (fixnum)
I have also problem, i try to add this value to array element :
convert_loadDOM = self.loadDOMstat
    n1 = convert_loadDOM[/Loading DOM finished: (\d+) ms/, 1]
    files_load_DOM[i]= files_load_DOM[i] + Integer(n1) 

    and i have error:
     undefined method `+' for nil:NilClass (NoMethodError)  

[toc] | [prev] | [next] | [standalone]


#6988

FromMichael Uplawski <michael.uplawski@uplawski.eu>
Date2014-06-03 19:51 +0200
Message-ID<slrnlos2o7.44b.michael.uplawski@drusus.uplawski.eu>
In reply to#6987
Good evening

On Tue, 3 Jun 2014 03:22:25 -0700 (PDT),
qoexcel@gmail.com <qoexcel@gmail.com> wrote:

> I have also problem, i try to add this value to array element :

Where in your code did you initialize 'files_load_DOM' to an
Array-object or other container-type with a '[]' method and add a first
element to it?

> convert_loadDOM = self.loadDOMstat
>     n1 = convert_loadDOM[/Loading DOM finished: (\d+) ms/, 1]
>     files_load_DOM[i]= files_load_DOM[i] + Integer(n1) 
      ~~~~~~~~~~~~~~~~~
Not here! 

>
>     and i have error:
>      undefined method `+' for nil:NilClass (NoMethodError)  

At least, that is, what the message points at.


Cherio,

Michael

-- 
GnuPG/OpenPGP  4096R/3216CF02 2013-11-15 [expires: 2015-11-15]
sub   4096R/2751C550 2013-11-15 [expires: 2015-11-15]

[toc] | [prev] | [next] | [standalone]


#6989

FromRobert Klemme <shortcutter@googlemail.com>
Date2014-06-03 22:53 +0200
Message-ID<bv6qu7Fnqn1U1@mid.individual.net>
In reply to#6987
On 03.06.2014 12:22, qoexcel@gmail.com wrote:
> вторник, 3 июня 2014 г., 0:11:38 UTC+4 пользователь Robert Klemme написал:
>> On 06/02/2014 10:07 PM, qoexcel@gmail.com wrote:
>>

> I have also problem, i try to add this value to array element :
> convert_loadDOM = self.loadDOMstat
>      n1 = convert_loadDOM[/Loading DOM finished: (\d+) ms/, 1]
>      files_load_DOM[i]= files_load_DOM[i] + Integer(n1)
>
>      and i have error:
>       undefined method `+' for nil:NilClass (NoMethodError)

files_load_DOM[i] ist most likely nil - either because i points past the 
last index of the Array or you never put a value there.

Kind regards

	robert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web