Ruby Forum Test > extraction and validation of "string text" & "number text"

Posted by Dave Lilley (dglnz)
on 24.03.2008 05:57
Hi i've got a wee problem - New to ruby using 1.86 and hope i might get
some pointers.

I have a file that i want to process and i'm hinting a particular
problem.

here is teh first 5 characters here...

a = A12a4

I have been able to break the line up by using  a[0..0] to test the 1st
field but i'm stuck on the 2nd 4 digit field.

Tried using split(//) so i can test each character but it's not clear
cut.
sample code snippet

 if a[1..4].to_i.integer? == false
   puts 'invalid entry!'
 else
   puts 'valid entry!'
 end

i cannot get a test that produces the answer i'm after which is if the
field is all numeric it passes else it fails!

tried a number of variations that turns the string into an array,
testing against ['A..Z' && 'a..z'] getting false both times regardless
of the data being valid or not.

thanks for any help.

ps I cannot use scan because the text is dynamic (well as far as my
limited knowledge allows me to believe).

dave.
Posted by Dave Lilley (dglnz)
on 04.04.2008 09:28
Dave Lilley wrote:
> Hi i've got a wee problem - New to ruby using 1.86 and hope i might get
> some pointers.
> 
> I have a file that i want to process and i'm hinting a particular
> problem.
> 
> here is teh first 5 characters here...
> 
> a = A12a4
> 
> I have been able to break the line up by using  a[0..0] to test the 1st
> field but i'm stuck on the 2nd 4 digit field.
> 
> Tried using split(//) so i can test each character but it's not clear
> cut.
> sample code snippet
> 
>  if a[1..4].to_i.integer? == false
>    puts 'invalid entry!'
>  else
>    puts 'valid entry!'
>  end
> 
> i cannot get a test that produces the answer i'm after which is if the
> field is all numeric it passes else it fails!
> 
> tried a number of variations that turns the string into an array,
> testing against ['A..Z' && 'a..z'] getting false both times regardless
> of the data being valid or not.
> 
> thanks for any help.
> 
> ps I cannot use scan because the text is dynamic (well as far as my
> limited knowledge allows me to believe).
> 
> dave.

Well as no has replied to this and i've actually found out somethings 
since posting this message, I'll put this in as it might help someone 
sometime.

1st

a = 'A12a4'

a[0..0] == 'A'... # means if the 1st character equals to the letter 
'A'...
a[[1..4] == ...  # tests the remaining data.

BUT a much better way would be to create a Hashed array (which was what 
i really wanted but didn't know you could declare it empty).

val{}  #create an empty hash

later on put in fields and values

val{field1,'value1'}
val{field2,'value2'}

therefore you can then use val{field1} = 'value1' which is much more 
meaningful
that a[0..0] or what where ever the field is with the data structure.

dave.
etc
Posted by Richard Forster (rforster)
on 07.05.2008 05:13
Hi Dave,

I think you want to use regular expressions with the =~ operator or the 
scan function. If you are testing for non-numerics use \D, for numerics 
\d

Examples, just using IRB:

 '123a' =~ /\D/  returns 3, which is the position of the third character
 '123' =~ /\D/   returns nil, because there are no non-numerics
 '123' =~ /\d/   returns zero, the position of the first numeric
 '12a3'.scan(/\d/) returns ["1", "2", "3"] - an array of digits in the 
string

if (my_number =~ /\D/) == nil
   puts 'is numeric'
end


Richard Forster

=> 3
irb(main):022:0> '123' =~ /\D/
=> nil
irb(main):023:0> '123' =~ /\D/






Dave Lilley wrote:
> Dave Lilley wrote:
>> Hi i've got a wee problem - New to ruby using 1.86 and hope i might get
>> some pointers.
>> 
>> I have a file that i want to process and i'm hinting a particular
>> problem.
>> 
>> here is teh first 5 characters here...
>> 
>> a = A12a4
>> 
>> I have been able to break the line up by using  a[0..0] to test the 1st
>> field but i'm stuck on the 2nd 4 digit field.
>> 
>> Tried using split(//) so i can test each character but it's not clear
>> cut.
>> sample code snippet
>> 
>>  if a[1..4].to_i.integer? == false
>>    puts 'invalid entry!'
>>  else
>>    puts 'valid entry!'
>>  end
>> 
>> i cannot get a test that produces the answer i'm after which is if the
>> field is all numeric it passes else it fails!
>> 
>> tried a number of variations that turns the string into an array,
>> testing against ['A..Z' && 'a..z'] getting false both times regardless
>> of the data being valid or not.
>> 
>> thanks for any help.
>> 
>> ps I cannot use scan because the text is dynamic (well as far as my
>> limited knowledge allows me to believe).
>> 
>> dave.
> 
> Well as no has replied to this and i've actually found out somethings 
> since posting this message, I'll put this in as it might help someone 
> sometime.
> 
> 1st
> 
> a = 'A12a4'
> 
> a[0..0] == 'A'... # means if the 1st character equals to the letter 
> 'A'...
> a[[1..4] == ...  # tests the remaining data.
> 
> BUT a much better way would be to create a Hashed array (which was what 
> i really wanted but didn't know you could declare it empty).
> 
> val{}  #create an empty hash
> 
> later on put in fields and values
> 
> val{field1,'value1'}
> val{field2,'value2'}
> 
> therefore you can then use val{field1} = 'value1' which is much more 
> meaningful
> that a[0..0] or what where ever the field is with the data structure.
> 
> dave.
> etc