bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: prefix increment operator does not work as expected


From: arnold
Subject: Re: prefix increment operator does not work as expected
Date: Fri, 26 Jun 2020 05:26:56 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hello.

Thank you for taking the time to send in a report.

Hyunho Cho<mug896@naver.com> wrote:
>
> GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)
>  Operating System: Ubuntu 20.04 LTS
>  Kernel: Linux 5.4.0-39-generic
>  Architecture: x86-64
>
> ### 1. "++" prefix increment operator does not work as expected
>
> $ LC_ALL=C awk 'BEGIN { n = 12; x = 2; print n x++ }'  # OK
> 122
>
> $ LC_ALL=C awk 'BEGIN { n = 12; x = 2; print n ++x }'  # not as expected
> 122

Spaces in awk are tricky.  Concatenation is specified by placing two
expressions adjacent to each other, space is not explicitly an operator.

Furthermore, space is ignored between operators and operands:

        $ gawk 'BEGIN { x = 12 ; print x ++ ; print x }'
        12
        13

In the case of your example gawk is parsing the code as

        BEGIN { n = 12; x = 2; print (n ++) x }

This explains what's happening in all your other examples
as well.

To get what you wanted, use parentheses or stick a null string
in the middle:

        $ gawk 'BEGIN { n = 12; x = 2; print (n) ++x }'
        123
        $ gawk 'BEGIN { n = 12; x = 2; print n "" ++x }'
        123

Thanks,

Arnold



reply via email to

[Prev in Thread] Current Thread [Next in Thread]