String - Number problems 
Author Message
 String - Number problems

Guys

I have a string that seems to be treated as a numeric.
When I "if { $str == $max_val }" I get Number too big errors.
and
when I transfer string to a proc, eg [get_det $string], string ends up
"500000".

I am in fact using "expect" to validate telephone number details
across a dozen or so systems, so the string is typically 32486123456
or 449791234567.

Any ideas ?

Regards
Ian



Fri, 30 Jul 2004 23:25:27 GMT  
 String - Number problems

Quote:

> Guys

> I have a string that seems to be treated as a numeric.
> When I "if { $str == $max_val }" I get Number too big errors.
> and
> when I transfer string to a proc, eg [get_det $string], string ends up
> "500000".

> I am in fact using "expect" to validate telephone number details
> across a dozen or so systems, so the string is typically 32486123456
> or 449791234567.

> Any ideas ?

> Regards
> Ian

Yes, == tries to use numbers if at all possible. Your sequences are
to large to fit into 32-bits integers. Several solutions are
available:

   if { "tel$str" == "tel$max_val" } ...

   if { [string equal $str $max_val] } ...

the first forces the number-like string into a string that can not
possibly be a string, the other does not even attempt to do the
conversion.

Regards,

Arjen



Fri, 30 Jul 2004 23:45:57 GMT  
 String - Number problems

Quote:

> Guys

> I have a string that seems to be treated as a numeric.
> When I "if { $str == $max_val }" I get Number too big errors.
> and
> when I transfer string to a proc, eg [get_det $string], string ends up
> "500000".

> I am in fact using "expect" to validate telephone number details
> across a dozen or so systems, so the string is typically 32486123456
> or 449791234567.

> Any ideas ?

if uses the same expression parser as expr which tries to treat strings like numbers
if they look like numbers - if you want string comparison use the string command
    if {[string equal $str $max_val]} {...
or if using new interp (8.4)
      if { $str eq $max_val } {...

just calling a proc won't do anything weird to a string - there must be more either
before or after the proc call ... a code example of this behavior will help.

Bruce



Fri, 30 Jul 2004 23:54:17 GMT  
 String - Number problems

Quote:

> Guys

> I have a string that seems to be treated as a numeric.
> When I "if { $str == $max_val }" I get Number too big errors.
> and
> when I transfer string to a proc, eg [get_det $string], string ends up
> "500000".

> I am in fact using "expect" to validate telephone number details
> across a dozen or so systems, so the string is typically 32486123456
> or 449791234567.

> Any ideas ?

> Regards
> Ian

Works with me:
% package require Expect
5.32.2
% set str 0498887261
0498887261
% set max_val 32486123456475869709483726
32486123456475869709483726
% if { $str == $max_val } {
puts yep
Quote:
} else { puts noop }

noop

You'll have to be more specific.

regards Peter.
--



Fri, 30 Jul 2004 23:43:57 GMT  
 String - Number problems

Quote:

> Guys

> I have a string that seems to be treated as a numeric.
> When I "if { $str == $max_val }" I get Number too big errors.

i prefer to compare strings explicitly as string

    [string equal $str $max_val]

on newer tcls and on older

    proc streq {a b} {expr {[string compare $a $b]==0}}
    [streq $str $max_val]



Sat, 31 Jul 2004 00:10:26 GMT  
 String - Number problems

Quote:

> I have a string that seems to be treated as a numeric.
> When I "if { $str == $max_val }" I get Number too big errors.
> and
> when I transfer string to a proc, eg [get_det $string], string ends up
> "500000".

> I am in fact using "expect" to validate telephone number details
> across a dozen or so systems, so the string is typically 32486123456
> or 449791234567.

> Any ideas ?

Don't ever use == to compare strings.  Use [string compare] and
[string equal] when you intend string comparisons, and you will have
much less trouble.

--
73 de ke9tv/2, Kevin KENNY      GE Corporate R&D, Niskayuna, New York, USA



Sat, 31 Jul 2004 02:23:56 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. number's to string -) tcp-ip -) scan from string problem

2. BUG: Scientific Number to String/String to Number

3. number to string .... string to number

4. Problem with STRING to NUMBER conversion

5. String Number to COMP-2 Conversion Problem

6. indexing numbers as numbers or strings

7. VPREXX Help? Adding number to string number

8. number to string to number

9. Using scan from string to convert a string into a number

10. Problem with string module and strings containing \0

11. Numbers Numbers Numbers

12. Q: String To Number Conversion

 

 
Powered by phpBB® Forum Software