bug-gawk
[Top][All Lists]
Advanced

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

Re: Statements as functions?


From: arnold
Subject: Re: Statements as functions?
Date: Mon, 24 Feb 2025 04:12:16 -0700
User-agent: Heirloom mailx 12.5 7/5/10

Hi.

Thank you for your note.

Jim Mellander <jmellander@gmail.com> wrote:

> My first post to the list, been using awk for decades...

Welcome. I'm glad it's been useful to you.

> I found a case where I would kinda like to execute a statement as a
> function, kinda like some other languages (lua, e.g.) do.
>
> Here's a fragment from a script that processes thttpd logs and creates a
> count of unique IPs per day hitting a webserver, excluding spiders:
>
> # Print date & IP if different from previous line of input
> #                                                    date " " ip
> ! ($1 in spider) && ((l = substr($4,2,11) " " $1) != lastline) {
>         print lastline = l
> }
>
> What would be nice (not essential by any means) would be to be able to code
> it all in the pattern section, like this:
>
> ! ($1 in spider) && ((l = substr($4,2,11) " " $1) != lastline && print
> lastline = l) { }

It would be nice, but it's mostly syntactic sugar and doesn't
save you any typing. In fact, it adds more characters.

It also breaks the "when this condition is true do this action"
nature of awk programs.

> print could return the count of characters printed. Similarly, a for loop
> could return the number of iterations. Obviously, a counter in the for loop
> could serve the same purpose, and a helper function to print can be used in
> my case (but not in the general case).
>
> So, this isn't really a feature request, but kind of a nice to have...

Unfortunately, the cost/benefit ratio here doesn't make this worth
doing. Making every statement (for, while, do, if, select) into
expressions would very seriously complicate the grammar. And for
a feature that would very likely see little or no use in practice.

Not to mention that gawk already has too many features that in retrospect
it could have done without.

So, I appreciate the suggestion, but I'm going to have to decline
to do anything about it.

Best wishes,

Arnold



reply via email to

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