gawk: conditional test ? if-true ? else expression question 
Author Message
 gawk: conditional test ? if-true ? else expression question

hello,

i tried

    x = 1
    x == 0 ? x ++ : x += 25
    printf("x - %d\n", x)
    exit

    output : 26

then i tried

    x = 0
    x == 0 ? x ++ : x += 25
    printf("x - %d\n", x)
    exit

    output : 1

this is great, and this is what i expected. why then, the following
does not work (gives me a parse error on printf) :

    x = 1
    x == 0 ? x ++ : printf "hello world\n"
    printf("x - %d\n", x)
    exit

from the manual, i could only tell that :

     selector ? if-true-exp : if-false-exp

but isn't printf() an expression? it is a function call,
but are function calls not expressions?

thanks,

denis



Fri, 20 Feb 2004 13:45:45 GMT  
 gawk: conditional test ? if-true ? else expression question

<snip>

Quote:
> this is great, and this is what i expected. why then, the following
> does not work (gives me a parse error on printf) :

>     x = 1
>     x == 0 ? x ++ : printf "hello world\n"
>     printf("x - %d\n", x)
>     exit

> from the manual, i could only tell that :

>      selector ? if-true-exp : if-false-exp

> but isn't printf() an expression? it is a function call,
> but are function calls not expressions?

The expression "x++" returns a value, but 'printf "hello world\n"' doesn't
AFAIK return a value [1].  The syntax calls for a value to be returned for
each part of the operator, otherwise it could not be used in
its "usual" format of,

    s = (x == 0) ? 1 : 0

to set the value of s.

[1] I deduce this because something like

    gawk 'BEGIN { s = printf "\n"; print s}'

gives a parse error for the printf statement.

HTH
--
Peter S Tillier

Opinions expressed are my own and not necessarily
those of my employer.



Fri, 20 Feb 2004 19:30:43 GMT  
 gawk: conditional test ? if-true ? else expression question

...

Quote:
>this is great, and this is what i expected. why then, the following
>does not work (gives me a parse error on printf) :

>    x = 1
>    x == 0 ? x ++ : printf "hello world\n"
>    printf("x - %d\n", x)
>    exit

>from the manual, i could only tell that :

>     selector ? if-true-exp : if-false-exp

>but isn't printf() an expression? it is a function call,
>but are function calls not expressions?

Function calls are expressions, but "print" and "printf" are not functions.
They are statements.  I think of "print" (and "printf") as parsing like "for"
and "while".  You wouldn't expect to be able to use these as expressions...


Fri, 20 Feb 2004 21:00:26 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Help with a conditional expression

2. conditional expressions

3. Proposed PEP for a Conditional Expression

4. No Assignment in Conditional/PROPOSAL: Assignment expression

5. history of conditional expressions

6. Conditional Expressions don't solve the problem

7. Another conditional expression candidate (PEP 308)

8. FW: [Python-Dev] conditional expressions?

9. 2nd Beta of gawk 3.1.2 now available for testing

10. beta test version of patch 2 to gawk 3.1 available

11. testing true condition

12. Gawk for win32 slower than Gawk for Dos_32?

 

 
Powered by phpBB® Forum Software