(numeric != string) and associated issue 
Author Message
 (numeric != string) and associated issue

I'm writing a script to check some value against reference.
the data can be string or numeric.

My script roughly looks like:

/somematch/ {
    dataref = $0
    somescript | getline
    data = $0

    if (data != dataref) print "diff found"

Quote:
}

now I could end up with (note I'm using . as decimal separator)

dataref = 100
data    = "100.0"

and I get data!=dataref, which is not what I want

okay I'd guess data is string and dataref numeric,

The only hack I found was using this before the test:

split(data,val,"")
data=val[1]

this stuff is pretty neat because it does the job: data is now a numeric
when I compare and I get the nmatch.
And it deosn't affect the rest:

eg.
rel-1.5 != rel-1.50   (thank god)

however I'm still stuck with this sort of case:
    1e-1 != 0.10

Sorry if the problem description is not too clear but if you have an
hindsight of what I'm saying and know how to solve that, please pop me
an e-mail

thanks,
Matthieu

--
 Matthieu Bec                       Isaac Newton Group
 tel  : +34 922 425427              Apartado de Correos 321
 fax  : +34 922 425429              38780 Santa Cruz de La Palma
 url  : www.ing.iac.es/~mdcb        Canary Islands, Spain



Mon, 16 Dec 2002 03:00:00 GMT  
 (numeric != string) and associated issue


Quote:

>--------------3DBC5417E7056279F347139D
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit

Please turn off MIME.

<snip>

Quote:
>dataref = 100
>data    = "100.0"

>and I get data!=dataref, which is not what I want

<snip>

If either or both dataref and data could be string or numeric, then
it's only really safe to perform both string and numeric comparisons in
sequence.

((data "" == dataref "") || (data + 0 == dataref + 0))

In the case above, the first comparison would fail but the second
succeed.

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 17 Dec 2002 03:00:00 GMT  
 (numeric != string) and associated issue

Quote:



>>--------------3DBC5417E7056279F347139D
>>Content-Type: text/plain; charset=us-ascii
>>Content-Transfer-Encoding: 7bit

> Please turn off MIME.

> <snip>

>>dataref = 100
>>data    = "100.0"

>>and I get data!=dataref, which is not what I want

> <snip>

> If either or both dataref and data could be string or numeric, then
> it's only really safe to perform both string and numeric comparisons in
> sequence.

> ((data "" == dataref "") || (data + 0 == dataref + 0))

> In the case above, the first comparison would fail but the second
> succeed.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

      $ awk '{
      > if (($1 "" == $2 "") || ($1+0 == $2+0))
      >    print "ok"
      > else
      >    print "bad"
      > }'
      0 0                    ---> input
      ok                     <--- output
      1.0 1                  ---> input
      ok                     <--- output
      a b                    ---> input
      ok                     <--- output
      a 0                    ---> input
      ok                     <--- output

--
Dan Mercer



Tue, 17 Dec 2002 03:00:00 GMT  
 (numeric != string) and associated issue

Quote:





>>>--------------3DBC5417E7056279F347139D
>>>Content-Type: text/plain; charset=us-ascii
>>>Content-Transfer-Encoding: 7bit

>> Please turn off MIME.

>> <snip>

>>>dataref = 100
>>>data    = "100.0"

>>>and I get data!=dataref, which is not what I want

>> <snip>

>> If either or both dataref and data could be string or numeric, then
>> it's only really safe to perform both string and numeric comparisons in
>> sequence.

>> ((data "" == dataref "") || (data + 0 == dataref + 0))

>> In the case above, the first comparison would fail but the second
>> succeed.

>> Sent via Deja.com http://www.deja.com/
>> Before you buy.

>      $ awk '{
>      > if (($1 "" == $2 "") || ($1+0 == $2+0))
>      >    print "ok"
>      > else
>      >    print "bad"
>      > }'
>      0 0                    ---> input
>      ok                     <--- output
>      1.0 1                  ---> input
>      ok                     <--- output
>      a b                    ---> input
>      ok                     <--- output
>      a 0                    ---> input
>      ok                     <--- output

And this shows more clearly what's happening:

for this infile:

0 0                    
1.0 1                
a b                  
a 0                

this awk code:

awk '{if (($1 "x" == $2 "x")) {print "ok string", $1, $2
                               print $1 "x", $2 "x"; next}
      if (($1 + 0 == $2 + 0)) {print "ok number", $1, $2
                               print $1 + 0, $2 + 0; next}
      print "bad", $1, $2 }' infile

produces this output:

ok string 0 0
0x 0x
ok number 1.0 1
1 1
ok number a b
0 0
ok number a 0
0 0

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 17 Dec 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Call for new function ? was:(numeric != string) and associated issue

2. Ada String Issue: String within Strings

3. Associating a string with a given numerical input

4. Associating strings with buffers?

5. Converting hex string values to numeric

6. Number to numeric string convertion

7. Is this how to format numeric strings?

8. Picture Tokens - Numeric to String Format

9. format numeric into string loses decimal?

10. String to Numeric

11. Find a numeric in a string C2003

12. Numeric to string

 

 
Powered by phpBB® Forum Software