convert script awk in python

Alan Gauld alan.gauld at yahoo.co.uk
Wed Mar 24 05:28:25 EDT 2021


On 23/03/2021 14:40, Avi Gross via Python-list wrote:

> $1 == 113 {
>     if (x || y || z)
>         print "More than one type $8 atom.";
>     else {
>         x = $2; y = $3; z = $4;
>         istep++;
>     }
> }
> 
> I am a tod concerned as to where any of the variables x, y or z have been
> defined at this point. 

They haven't been, they are using awk's auto-initialization feature.
The variables are defined in this bit of code. The first time we see $1
== 113 we define the variables. On subsequent appearances we print the
warning.

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

It's standard behaviour in any POSIX compliant awk, variables are
initialised to empty strings/arrays or zero as appropriate to first use.

The original AWK book has already been mentioned, which covers nawk.
I'll add the O'Reilly book "sed & awk" which covers the POSIX version
and includes several extensions not covered in the original book. (It
also covers sed but that's irrelevant here)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Python-list mailing list