convert script awk in python

Cameron Simpson cs at cskk.id.au
Tue Mar 23 18:38:22 EDT 2021


On 23Mar2021 16:37, Tomasz Rola <rtomek at ceti.pl> wrote:
>On Tue, Mar 23, 2021 at 10:40:01AM -0400, Avi Gross via Python-list wrote:
>[...]
>> I am a tod concerned as to where any of the variables x, y or z have been
>> defined at this point. I have not seen a BEGIN {...} pattern/action or
>> anywhere these have been initialized but they are set in a function that as
>> far as I know has not been called. Weird. Maybe awk is allowing an
>> uninitialized variable to be tested for in your code but if so, you need to
>> be cautious how you do this in python.
>
>As far as I can say, the type of uninitialised variable is groked from
>the first operation on it. I.e., "count += 1" first initializes count
>to 0 and then adds 1.
>
>This might depend on exact awk being used. There were few of them
>during last 30+ years. I just assume it does as I wrote above.

I'm pretty sure this behaviour's been there since very early times. I 
think it was there when I learnt awk, decades ago.

>Using BEGIN would be in better style, of course.

Aye. Always good to be up front about initial values.

>There is a very nice book, "The AWK Programming Language" by Aho,
>Kernighan and Weinberger. First printed in 1988, now free and in pdf
>format. Go search.

Yes, a really nice book. [... walks into the other room to get his copy 
...] October 1988.

Wow. There're 11 pages of good example programmes before any need for 
user variables at all. But at "1.5, Counting" is the sentence:

    Awk variables used as numbers begin life with the value 0, so we 
    don't need to initialise emp.

Which is great for writing ad hoc scripts, particularly on the command 
line. But not a great style for anything complex.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list