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

Thanks to everyone's posting, you answered my question

but at the same time I guess the final answer is what I'm trying to do
is not really suited to awk
- which comes a bit as a surprise !! I would have thought I was on a
typical awk problem.

Note the "real data" regexp, still get stuck on hexadecimals ..

in Tcl (which I wouldn't assume to be the langage of predilection for
this kind of processing),
all these regex business would translate into:

if { $a == $b } ...

this simple statement handles everything I had to deal with.

Okay I guess it goes beyond the scope of string matching and regexp,
but wouldn't a new "nequal" kind of function be handy on many occasion
for all awk aficinonados ?

Matthieu

==================================

Quote:

> 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"
> }

> 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,
<...omitted...>
> 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:
    As I understand it, you want to perform an arithmetic comparison
iff both operands are strictly numeric values, or a string comparision
otherwise.  Judicious use of a (real) number-matching RE should solve
the problem; the following passes every test I could think of:

nawk  '
 BEGIN {
  # real_num ::
  #  sign     integer  decimal     integer     exponent
  #      opt                  opt         opt          opt
  #
  #   | sign     decimal     integer  exponent
  #      opt         opt                   opt
  #
    real_num = "/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/"
  #   XXX could not get this to work as dynamic regexp
 }

 /./ {
    dataref = $2
    data = $1
  # if ((data ~ real_num) && (dataref ~ real_num)) # XXX
    if ((data~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/) &&
     (dataref~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/)) {
        # compare arithmetically
        equal = ((data+0) == (dataref+0))
        comparison = "number"
    } else {
        # compare lexically
        equal = ((data "") == (dataref ""))
        comparison = "string"
    }
    # if (!equal) print "diff found"
    printf("%s %ss:\t", ((equal) ? "same" : "diff"), comparison)
    print $1 " " $2

 0       0
 1.0     1
 1e-1    0.10
 3.99999 .4e+01
 -32     -32g7
 a       b
 a       0
 alike   alike

same numbers:   0 0
same numbers:   1.0 1
same numbers:   1e-1 0.10
diff numbers:   3.99999 .4e+01
diff strings:   -32 -32g7
diff strings:   a b
diff strings:   a 0
same strings:   alike alike
diff strings:   foo

    Hope this helps!
     - CJH / esper

Quote:
>  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

--
/*********************************************************************\
* Christopher J. Hawley / esper   sys_admin, acting DBA, troublemaker *
* c/o: Space Systems / L{*filter*}                                         *

*      Palo Alto, CA  94303-4604     phone: 1-650-852-6180  (work)    *
\***** "If we knew what we were doing, it wouldn't be research." *****/

#include        "ObDisclaimer.h"
   /* I have my own opinions; L{*filter*}has theirs.  Unrelated sets. */

Matthieu:
    As I understand it, you want to perform an arithmetic comparison
iff both operands are strictly numeric values, or a string comparision
otherwise.  Judicious use of a (real) number-matching RE should solve
the problem; the following passes every test I could think of:

nawk  '
 BEGIN {
  # real_num ::
  #  sign     integer  decimal     integer     exponent
  #      opt                  opt         opt          opt
  #
  #   | sign     decimal     integer  exponent
  #      opt         opt                   opt
  #
    real_num = "/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/"
  #   XXX could not get this to work as dynamic regexp
 }

 /./ {
    dataref = $2
    data = $1
  # if ((data ~ real_num) && (dataref ~ real_num)) # XXX
    if ((data~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/) &&
     (dataref~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/)) {
        # compare arithmetically
        equal = ((data+0) == (dataref+0))
        comparison = "number"
    } else {
        # compare lexically
        equal = ((data "") == (dataref ""))
        comparison = "string"
    }
    # if (!equal) print "diff found"
    printf("%s %ss:\t", ((equal) ? "same" : "diff"), comparison)
    print $1 " " $2

 0       0
 1.0     1
 1e-1    0.10
 3.99999 .4e+01
 -32     -32g7
 a       b
 a       0
 alike   alike

same numbers:   0 0
same numbers:   1.0 1
same numbers:   1e-1 0.10
diff numbers:   3.99999 .4e+01
diff strings:   -32 -32g7
diff strings:   a b
diff strings:   a 0
same strings:   alike alike
diff strings:   foo

    Hope this helps!
     - CJH / esper

--
 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



Sat, 28 Dec 2002 03:00:00 GMT  
 Call for new function ? was:(numeric != string) and associated issue

Thanks to everyone's posting, you answered my question

but at the same time I guess the final answer is what I'm trying to do
is not really suited to awk
- which comes a bit as a surprise !! I would have thought I was on a
typical awk problem.

Note the "real data" regexp, still get stuck on hexadecimals ..

in Tcl (which I wouldn't assume to be the langage of predilection for
this kind of processing),
all these regex business would translate into:

if { $a == $b } ...

this simple statement handles everything I had to deal with.

Okay I guess it goes beyond the scope of string matching and regexp,
but wouldn't a new "nequal" kind of function be handy on many occasion
for all awk aficinonados ?

Matthieu

==================================

Quote:

> 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"
> }

> 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,
<...omitted...>
> 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:
    As I understand it, you want to perform an arithmetic comparison
iff both operands are strictly numeric values, or a string comparision
otherwise.  Judicious use of a (real) number-matching RE should solve
the problem; the following passes every test I could think of:

nawk  '
 BEGIN {
  # real_num ::
  #  sign     integer  decimal     integer     exponent
  #      opt                  opt         opt          opt
  #
  #   | sign     decimal     integer  exponent
  #      opt         opt                   opt
  #
    real_num = "/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/"
  #   XXX could not get this to work as dynamic regexp
 }

 /./ {
    dataref = $2
    data = $1
  # if ((data ~ real_num) && (dataref ~ real_num)) # XXX
    if ((data~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/) &&
     (dataref~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/)) {
        # compare arithmetically
        equal = ((data+0) == (dataref+0))
        comparison = "number"
    } else {
        # compare lexically
        equal = ((data "") == (dataref ""))
        comparison = "string"
    }
    # if (!equal) print "diff found"
    printf("%s %ss:\t", ((equal) ? "same" : "diff"), comparison)
    print $1 " " $2

 0       0
 1.0     1
 1e-1    0.10
 3.99999 .4e+01
 -32     -32g7
 a       b
 a       0
 alike   alike

same numbers:   0 0
same numbers:   1.0 1
same numbers:   1e-1 0.10
diff numbers:   3.99999 .4e+01
diff strings:   -32 -32g7
diff strings:   a b
diff strings:   a 0
same strings:   alike alike
diff strings:   foo

    Hope this helps!
     - CJH / esper

Quote:
>  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

--
/*********************************************************************\
* Christopher J. Hawley / esper   sys_admin, acting DBA, troublemaker *
* c/o: Space Systems / L{*filter*}                                         *

*      Palo Alto, CA  94303-4604     phone: 1-650-852-6180  (work)    *
\***** "If we knew what we were doing, it wouldn't be research." *****/

#include        "ObDisclaimer.h"
   /* I have my own opinions; L{*filter*}has theirs.  Unrelated sets. */

Matthieu:
    As I understand it, you want to perform an arithmetic comparison
iff both operands are strictly numeric values, or a string comparision
otherwise.  Judicious use of a (real) number-matching RE should solve
the problem; the following passes every test I could think of:

nawk  '
 BEGIN {
  # real_num ::
  #  sign     integer  decimal     integer     exponent
  #      opt                  opt         opt          opt
  #
  #   | sign     decimal     integer  exponent
  #      opt         opt                   opt
  #
    real_num = "/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/"
  #   XXX could not get this to work as dynamic regexp
 }

 /./ {
    dataref = $2
    data = $1
  # if ((data ~ real_num) && (dataref ~ real_num)) # XXX
    if ((data~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/) &&
     (dataref~/^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][+-]?[0-9]+)?$/)) {
        # compare arithmetically
        equal = ((data+0) == (dataref+0))
        comparison = "number"
    } else {
        # compare lexically
        equal = ((data "") == (dataref ""))
        comparison = "string"
    }
    # if (!equal) print "diff found"
    printf("%s %ss:\t", ((equal) ? "same" : "diff"), comparison)
    print $1 " " $2

 0       0
 1.0     1
 1e-1    0.10
 3.99999 .4e+01
 -32     -32g7
 a       b
 a       0
 alike   alike

same numbers:   0 0
same numbers:   1.0 1
same numbers:   1e-1 0.10
diff numbers:   3.99999 .4e+01
diff strings:   -32 -32g7
diff strings:   a b
diff strings:   a 0
same strings:   alike alike
diff strings:   foo

    Hope this helps!
     - CJH / esper

--
 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



Sat, 28 Dec 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. (numeric != string) and associated issue

2. VC++ calling fortran function and fortran function calling a c++ function

3. Ada String Issue: String within Strings

4. I am not able to execute new.ccf in coboldpc when I call coboldpc through labview

5. How to find out name of calling function from called function

6. Does any language allow calling strings as functions?

7. Does any language allow calling strings as functions?

8. string function called by C challenge

9. call of non-function (type string) error

10. Calling Python functions from C from strings

11. Call a function in DLL with string arguments?

12. New function generation from strings ?

 

 
Powered by phpBB® Forum Software