Re: {} Questions

Liste des GroupesRevenir à cl awk 
Sujet : Re: {} Questions
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.awk
Date : 21. Aug 2024, 16:38:03
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87v7zt28f8.fsf@bsb.me.uk>
References : 1 2 3
User-Agent : Gnus/5.13 (Gnus v5.13)
porkchop@invalid.foo (Mike Sanders) writes:

Ben Bacarisse <ben@bsb.me.uk> wrote:
>
porkchop@invalid.foo (Mike Sanders) writes:
 
Assuming any awk variant...
>
1. Is this valid? (it works with mawk, gawk, busy box awk)
>
   BEGIN { debug = 1 }
>
   (debug)  { code_here }
>
   (!debug) { code_here }
>
   END { ... }
 
Yes, that's valid.  You don't need the ()s round the expressions.
>
Thanks Ben.
>
2. what is the name or accepted term in AWK for unamed functions,
main()?
 
I don't know what you mean.  Can you give an example?
 
It occurs to me that maybe you think
 
  (debug) { code }
 
is a function?  It's not.  It's just a normal pattern/action AWK pair.
An AWK pattern can just be an expression.  That expression is evaluated
for every input line and, if true, the corresponding action is executed.
>
And yet there's still more implied nuance somehow. Let me try to articulate
my thoughts...
>
- These types of constructs are 'auto' ran per line of input (assuming
  its not located within another user-written function) that I get.
  Perhaps a potential efficiency hit to be aware of.

AWK does not have the usual nested syntax you have probably come to
expect.  In, for example, Algol 68 blocks are made up of statements that
can include procedure declarations that include blocks make up of
statements and so on to whatever level of nest you might want.

AWK's top-level syntax -- a list of function declarations and
pattern/action pairs -- is never nested.  It's the outer syntax of a AWK
program and that's it.  None of it can appear inside any other AWK
construct.

- There's also the scope of variables to consider... Because any
  variable's located outside of a user-written function or conversely
  located within a 'bare naked' construct is global, or at least
  exposed to the entire script's 'world', so I want be careful here...

I think your "conversely" is misplaced.  And some variables inside a
user-defined functions are also global; the exceptions are names written
as parameters to a function, which I think is what you are saying
below.

  I'm guessing that any construct that lacks a function signature
  is globally scoped:
>
  { im_global = 42 }

This does not "lack a function signature", or at least that's a very odd
way of putting it.  It is, presumably, the action of a pattern/action
pair (with maybe an empty pattern).

  while those with a function signature (located within parentheses)
  are locally scoped:
>
  function private(private_var1) { private_var1 = "foo" }

Yes.  The names listed as parameters in the function declaration are
local to the function and denote local variables.  That's why AWK
programmers have to play tricks like adding extra parameters to get
local variables.

- And 2 more...
>
  $ awk '{ v="im_global" }'
>
  $ awk -f foo -v global=55
>
  Anything within the above 2 are global as well?

Yes, everything is global except for the names parameters in a function
declaration.

Just thinking aloud here so, I'll let you long time posters describe
it more lucidly.

--
Ben.

Date Sujet#  Auteur
21 Aug 24 * {} Questions9Mike Sanders
21 Aug 24 `* Re: {} Questions8Ben Bacarisse
21 Aug 24  `* Re: {} Questions7Mike Sanders
21 Aug 24   +* Re: {} Questions4Janis Papanagnou
21 Aug 24   i+- Re: {} Questions1Janis Papanagnou
21 Aug 24   i`* Re: {} Questions2Mike Sanders
22 Aug 24   i `- Re: {} Questions1Janis Papanagnou
21 Aug 24   `* Re: {} Questions2Ben Bacarisse
21 Aug 24    `- Re: {} Questions1Mike Sanders

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal