Call for new function ? expr() 
Author Message
 Call for new function ? expr()

Quote:
> Just restate the problem in a different way that 'awk' can do.

right, I know there's probably some solutions, some realy nice regexp
...
how painfull is what reallly matters for me now.

personnaly - being lazy - I opted for:

   expr = sprintf ("echo \"puts [expr %s == %s]\" | tclsh",ref,val)
   expr | getline
   close(expr)
   truth = $0

My point was, maybe what's needed is a new expr() function, rather than
having to rely on <whatever>
Not to mention - I'd assume - the performance issue ...

Being well advanced in the script - and lazy - I didn't quite fancy
restarting from scratch using <whatever>, but I guess I've learnt my
lesson.

voila... just wanted to raise the issue,

M.

Quote:
>From: Mike

>Subject: Re: Call for new function ? was:(numeric != string) and
associated issue


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

>Just restate the problem in a different way that 'awk' can do.

>> in Tcl (which I wouldn't assume to be the langage of predilection for
>> this kind of processing),

>Why?  Tcl is good for lots of things, despite what the Perl and python
>pundits would like us to believe.

>> all these regex business would translate into:

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

>> this simple statement handles everything I had to deal with.

>That's because that Tcl expression is really calling "expr"
>which tries to convert its arguments to numbers, and does a numeric
>comparison.  It falls back to a string comparison if the arguments
>are not numbers.  Sounds like we could do that in 'awk', too!

> a1 = a + 0  # cast to number
> b1 = b + 0  # cast to number
> a2 = a ""  # cast to string
> b2 = b ""  # cast to string
> if ( a1 == b1 || a2 == b2 ) {
>  # same
> } else {
>  # different
> }

>But, alas, even that doesn't work, because 'awk' doesn't convert
>numbers for us (it doesn't change '077' to 63, nor '0x10' to 16).
>On the other hand, it's not too hard to write our own 'scanf()'
routine!
>;-)

>--
>Michael

 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

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



Wed, 01 Jan 2003 03:00:00 GMT  
 Call for new function ? expr()
I have not seen the original problem here. Is the problem really that you
are trying to compare numbers in different bases, or something like 10 vs
10.0?

% > a1 = a + 0  # cast to number
% > b1 = b + 0  # cast to number

There generally speaking is no need to do this with current awk
implementations. If this actually resolves the problem, then I
suggest using a more recent implementation. If you're on Solaris,
this means using nawk (or /usr/xpg4/bin/awk) instead of /usr/bin/awk.

 $1 == $2 { print "match"; next }
 { print "unmatch" }

Gives

 match
 match
 unmatch
 match
 unmatch

for the input

 1 1
 10 10.0
 10 10.1
 same same
 same different

Using any awk implemented in the last decade or so.
--

Patrick TJ McPhee
East York  Canada



Wed, 01 Jan 2003 03:00:00 GMT  
 Call for new function ? expr()

here's a (short) list of things I'd like to compare and get a match

10
10.000000
10.0000
0xa
1e1

etc.

note my input is not only numeric, I can also get strings coming, as in:

v1.1
STRING
what ever else

so far the best we've come too is compare numerical cast and string
exression,
but this leaves some caveat for hex, oct etc..

Quote:

> I have not seen the original problem here. Is the problem really that you
> are trying to compare numbers in different bases, or something like 10 vs
> 10.0?

> % > a1 = a + 0  # cast to number
> % > b1 = b + 0  # cast to number

> There generally speaking is no need to do this with current awk
> implementations. If this actually resolves the problem, then I
> suggest using a more recent implementation. If you're on Solaris,
> this means using nawk (or /usr/xpg4/bin/awk) instead of /usr/bin/awk.

>  $1 == $2 { print "match"; next }
>  { print "unmatch" }

> Gives

>  match
>  match
>  unmatch
>  match
>  unmatch

> for the input

>  1 1
>  10 10.0
>  10 10.1
>  same same
>  same different

> Using any awk implemented in the last decade or so.
> --

> Patrick TJ McPhee
> East York  Canada


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


Fri, 03 Jan 2003 03:00:00 GMT  
 Call for new function ? expr()


% here's a (short) list of things I'd like to compare and get a match
%
% 10
% 10.000000
% 10.0000
% 0xa
% 1e1

Except for the hexadecimal, all of these compare using current versions of
nawk, gawk, and mawk. There is no need to `cast' anything. To handle
hex, you'd need to detect hex values (do they always start with 0x?), and
use a function to convert them (many have been posted to this group).

If you find that $1 == $2 is not true when $1 is 10 and R2 is, eg, 1e1,
then you are probably using an old awk, and you should see whether nawk
is installed on your system. The hex conversion thing is too bad, but
it's got to be cleaner to call a function to do the conversion than
it is to always fire up another language which happens to accept numbers
using C's conventions.
--

Patrick TJ McPhee
East York  Canada



Sat, 04 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

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

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

3. Calling functions from functions from functions ...

4. PyType_GenericNew not calling Init/New functions

5. X != [expr tan([expr atan(X)])] ????

6. TIP #182: Add [expr bool] Math Function

7. tcl8.5 new operator in expr

8. UPDATE TCL-Compiler : New results for expr and string

9. Call Library Function - Function not found in library

10. C functions calling {C++/Ada/...} functions

11. What function called this function ??

12. Function called from the Listener or from another function

 

 
Powered by phpBB® Forum Software