• Is it possible to convert strings to math operators?

    From someone@21:1/5 to All on Thu Feb 16 08:02:31 2023
    I didn't see anything in the gawk(1) manpage for doing this but it seems
    like it should be possible.

    ex. oz to lb using old-school units(1) command:
    --
    $ units -1 oz lb
    * 0.0625

    # this works:
    $ echo "16 $(units -1 oz lb)" |awk '{print $1 " oz = " $1 * $3 " lb"}'
    16 oz = 1 lb

    # this does NOT work:
    $ echo "16 $(units -1 oz lb)" |awk '{print $1 " oz = " $1 $2 $3 " lb"}'
    16 oz = 16*0.0625 lb

    --

    Is there a way to tell (g)awk to interpret strings like "*", "/", "+",
    "-", etc. as actual math operators or do I need to create a series of
    tests of $2 in the above example?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to someone@invalid.invalid on Thu Feb 16 15:36:59 2023
    In article <tslgi7$3o0$1@nnrp.usenet.blueworldhosting.com>,
    someone <someone@invalid.invalid> wrote:

    ex. oz to lb using old-school units(1) command:

    $ units -1 oz lb
    * 0.0625

    # this works:
    $ echo "16 $(units -1 oz lb)" |awk '{print $1 " oz = " $1 * $3 " lb"}'
    16 oz = 1 lb

    # this does NOT work:
    $ echo "16 $(units -1 oz lb)" |awk '{print $1 " oz = " $1 $2 $3 " lb"}'
    16 oz = 16*0.0625 lb

    Is there a way to tell (g)awk to interpret strings like "*", "/", "+",
    "-", etc. as actual math operators

    The short answer is "No". The slightly longer answer is "No, awk does not
    have eval".

    or do I need to create a series of
    tests of $2 in the above example?

    You could switch to Perl (which does have eval).

    --
    The randomly chosen signature file that would have appeared here is more than 4 lines long. As such, it violates one or more Usenet RFCs. In order to remain in compliance with said RFCs, the actual sig can be found at the following URL:
    http://user.xmission.com/~gazelle/Sigs/Pearls

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to someone@invalid.invalid on Thu Feb 16 16:47:08 2023
    In article <tsll3n$2drb$1@nnrp.usenet.blueworldhosting.com>,
    someone <someone@invalid.invalid> wrote:
    On 2/16/23 8:36 AM, Kenny McCormack wrote:
    The short answer is "No". The slightly longer answer is "No, awk does not >> have eval".

    Ah okay thanks; guess I wasn't missing something in the docs after all.

    I don't really have practical reason for needing an eval() function, was
    just something I came across while trying other things.

    Yes, I get it. Although I've often wished AWK did have eval capability,
    I've never really wanted it enough for it to be a deal-breaker. So, you
    learn to live without it.

    Incidentally, I was once told by an AWK developer that the hard part about implementing eval is if the thing you're eval'ing causes a new variable to spring into existence.

    --
    Trump - the President for the rest of us.

    https://www.youtube.com/watch?v=JSkUJKgdcoE

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From someone@21:1/5 to Kenny McCormack on Thu Feb 16 09:20:07 2023
    On 2/16/23 8:36 AM, Kenny McCormack wrote:
    The short answer is "No". The slightly longer answer is "No, awk does not have eval".

    Ah okay thanks; guess I wasn't missing something in the docs after all.

    I don't really have practical reason for needing an eval() function, was
    just something I came across while trying other things.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to someone on Thu Feb 16 13:16:36 2023
    On 2/16/2023 9:02 AM, someone wrote:
    I didn't see anything in the gawk(1) manpage for doing this but it seems
    like it should be possible.


    It's not possible entirely within 1 call to awk but see https://stackoverflow.com/a/54161251/1745001 for a (slow) workaround.

    Ed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From someone@21:1/5 to Ed Morton on Thu Feb 16 18:52:59 2023
    On 2/16/23 12:16 PM, Ed Morton wrote:
    On 2/16/2023 9:02 AM, someone wrote:
    I didn't see anything in the gawk(1) manpage for doing this but it
    seems like it should be possible.


    It's not possible entirely within 1 call to awk but see https://stackoverflow.com/a/54161251/1745001 for a (slow) workaround.

        Ed.

    Interesting -- I had been thinking about this earlier actually but I'm
    not yet proficient enough to write functions like this. Bookmarked!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kpop 2GM@21:1/5 to All on Wed Mar 8 12:44:44 2023
    although these strictly aren't "math operators", you can build an eval() function specifically for relational operators with a very short syntax :

    function compare(__, _, ___) {
    …. return +__==+___ \
    ….…. ? _ ~ "[^!]=" \
    ….…. : (_ ~ "[!=].|.>" || +__<+___)==(_<"=")
    }


    0x1.0FFFFp+22. <>. 11141110000000000e-10. . 1. 4456444. 1114111
    0x1.0FFFFp+22. ==. 11141110000000000e-10. . 0. 4456444. 1114111
    0x1.0FFFFp+22. !=. 11141110000000000e-10. . 1. 4456444. 1114111

    0x1.0FFFFp+22. >. 11141110000000000e-10. . 1. 4456444. 1114111
    0x1.0FFFFp+22. >=. 11141110000000000e-10. . 1. 4456444. 1114111
    0x1.0FFFFp+22. <=. 11141110000000000e-10. . 0. 4456444. 1114111
    0x1.0FFFFp+22. <. 11141110000000000e-10. . 0. 4456444. 1114111

    0x1.0FFFFp+21. <>. 11141110000000000e-10. . 1. 2228222. 1114111
    0x1.0FFFFp+21. ==. 11141110000000000e-10. . 0. 2228222. 1114111
    0x1.0FFFFp+21. !=. 11141110000000000e-10. . 1. 2228222. 1114111

    0x1.0FFFFp+21. >. 11141110000000000e-10. . 1. 2228222. 1114111
    0x1.0FFFFp+21. >=. 11141110000000000e-10. . 1. 2228222. 1114111
    0x1.0FFFFp+21. <=. 11141110000000000e-10. . 0. 2228222. 1114111
    0x1.0FFFFp+21. <. 11141110000000000e-10. . 0. 2228222. 1114111

    0x1.0FFFFp+20. <>. 11141110000000000e-10. . 0. 1114111. 1114111
    0x1.0FFFFp+20. ==. 11141110000000000e-10. . 1. 1114111. 1114111
    0x1.0FFFFp+20. !=. 11141110000000000e-10. . 0. 1114111. 1114111

    0x1.0FFFFp+20. >. 11141110000000000e-10. . 0. 1114111. 1114111
    0x1.0FFFFp+20. >=. 11141110000000000e-10. . 1. 1114111. 1114111
    0x1.0FFFFp+20. <=. 11141110000000000e-10. . 1. 1114111. 1114111
    0x1.0FFFFp+20. <. 11141110000000000e-10. . 0. 1114111. 1114111

    0x1.0FFFFp+20. <>. 1114111000000000117E-12. 1. 1114111. 1114111.0000000002 0x1.0FFFFp+20. ==. 1114111000000000117E-12. 0. 1114111. 1114111.0000000002 0x1.0FFFFp+20. !=. 1114111000000000117E-12. 1. 1114111. 1114111.0000000002

    0x1.0FFFFp+20. >. 1114111000000000117E-12. 0. 1114111. 1114111.0000000002 0x1.0FFFFp+20. >=. 1114111000000000117E-12. 0. 1114111. 1114111.0000000002 0x1.0FFFFp+20. <=. 1114111000000000117E-12. 1. 1114111. 1114111.0000000002 0x1.0FFFFp+20. <. 1114111000000000117E-12. 1. 1114111. 1114111.0000000002

    0x1.0FFFFp+19. <>. 11141110000000000e-10. . 1. 557055.5. 1114111
    0x1.0FFFFp+19. ==. 11141110000000000e-10. . 0. 557055.5. 1114111
    0x1.0FFFFp+19. !=. 11141110000000000e-10. . 1. 557055.5. 1114111

    0x1.0FFFFp+19. >. 11141110000000000e-10. . 0. 557055.5. 1114111
    0x1.0FFFFp+19. >=. 11141110000000000e-10. . 0. 557055.5. 1114111
    0x1.0FFFFp+19. <=. 11141110000000000e-10. . 1. 557055.5. 1114111
    0x1.0FFFFp+19. <. 11141110000000000e-10. . 1. 557055.5. 1114111

    ** the relational operator "<>" is treated as NOT_EQUAL "!=" even though it's not part of awk syntax

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)