Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.awk > #9808 > unrolled thread

{} Questions

Started byporkchop@invalid.foo (Mike Sanders)
First post2024-08-21 05:35 +0000
Last post2024-08-21 19:01 +0000
Articles 9 — 3 participants

Back to article view | Back to comp.lang.awk


Contents

  {} Questions porkchop@invalid.foo (Mike Sanders) - 2024-08-21 05:35 +0000
    Re: {} Questions Ben Bacarisse <ben@bsb.me.uk> - 2024-08-21 09:06 +0100
      Re: {} Questions porkchop@invalid.foo (Mike Sanders) - 2024-08-21 11:48 +0000
        Re: {} Questions Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-08-21 14:08 +0200
          Re: {} Questions Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-08-21 14:10 +0200
          Re: {} Questions porkchop@invalid.foo (Mike Sanders) - 2024-08-21 18:57 +0000
            Re: {} Questions Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-08-22 00:51 +0200
        Re: {} Questions Ben Bacarisse <ben@bsb.me.uk> - 2024-08-21 16:38 +0100
          Re: {} Questions porkchop@invalid.foo (Mike Sanders) - 2024-08-21 19:01 +0000

#9808 — {} Questions

Fromporkchop@invalid.foo (Mike Sanders)
Date2024-08-21 05:35 +0000
Subject{} Questions
Message-ID<va3ua5$3ohv3$2@dont-email.me>
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 { ... }

2. what is the name or accepted term in AWK for unamed functions, main()?

-- 
:wq
Mike Sanders

[toc] | [next] | [standalone]


#9809

FromBen Bacarisse <ben@bsb.me.uk>
Date2024-08-21 09:06 +0100
Message-ID<87o75mpaec.fsf@bsb.me.uk>
In reply to#9808
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.

> 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.
You could have written

  debug != 0 { code }

instead.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#9811

Fromporkchop@invalid.foo (Mike Sanders)
Date2024-08-21 11:48 +0000
Message-ID<va4k6f$3rl80$1@dont-email.me>
In reply to#9809
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.

- 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'm guessing that any construct that lacks a function signature
  is globally scoped:

  { im_global = 42 }

  while those with a function signature (located within parentheses)
  are locally scoped:

  function private(private_var1) { private_var1 = "foo" }

- And 2 more...

  $ awk '{ v="im_global" }'

  $ awk -f foo -v global=55

  Anything within the above 2 are global as well?

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

-- 
:wq
Mike Sanders

[toc] | [prev] | [next] | [standalone]


#9812

FromJanis Papanagnou <janis_papanagnou+ng@hotmail.com>
Date2024-08-21 14:08 +0200
Message-ID<va4lb8$3rq72$1@dont-email.me>
In reply to#9811
On 21.08.2024 13:48, Mike Sanders wrote:
> 

[ Concerning the basic awk syntax:  condition { action }  ]

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

You cannot have these constructs with their given semantics inside a
function. You'd have to formulate them explicitly (in the imperative
form) with 'if', as in

  function f ()
  {
      if (condition) action
  }

>   Perhaps a potential efficiency hit to be aware of.
> 
> - 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...

All variables have global scope, with the exception of those specified
in a function argument list along with the real arguments, as in

  function f (arg1, arg2,   local1, local2)  { global = arg1 ; ... }
  f ("Hello", 42);

(There's some caveat with arrays in the function argument list.)

Janis

> 
>   I'm guessing that any construct that lacks a function signature
>   is globally scoped:
> 
>   { im_global = 42 }
> 
>   while those with a function signature (located within parentheses)
>   are locally scoped:
> 
>   function private(private_var1) { private_var1 = "foo" }
> 
> - And 2 more...
> 
>   $ awk '{ v="im_global" }'
> 
>   $ awk -f foo -v global=55
> 
>   Anything within the above 2 are global as well?
> 
> Just thinking aloud here so, I'll let you long time posters describe
> it more lucidly.
> 

[toc] | [prev] | [next] | [standalone]


#9813

FromJanis Papanagnou <janis_papanagnou+ng@hotmail.com>
Date2024-08-21 14:10 +0200
Message-ID<va4leo$3rq72$2@dont-email.me>
In reply to#9812
On 21.08.2024 14:08, Janis Papanagnou wrote:
> 
>   function f (arg1, arg2,   local1, local2)  { global = arg1 ; ... }
>   f ("Hello", 42);

    f("Hello", 42);

Of course no space between the identifier and the parenthesis with
user-defined functions.

Janis

[toc] | [prev] | [next] | [standalone]


#9815

Fromporkchop@invalid.foo (Mike Sanders)
Date2024-08-21 18:57 +0000
Message-ID<va5das$3vg5l$1@dont-email.me>
In reply to#9812
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:

> On 21.08.2024 13:48, Mike Sanders wrote:
>> 
> 
> [ Concerning the basic awk syntax:  condition { action }  ]
> 
>> 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.
> 
> You cannot have these constructs with their given semantics inside a
> function. You'd have to formulate them explicitly (in the imperative
> form) with 'if', as in
> 
>   function f ()
>   {
>       if (condition) action
>   }

Of course I tried & failed eariler today...

   function foo() {

      !boo { code }
   }
 
>>   Perhaps a potential efficiency hit to be aware of.
>> 
>> - 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...
> 
> All variables have global scope, with the exception of those specified
> in a function argument list along with the real arguments, as in
> 
>   function f (arg1, arg2,   local1, local2)  { global = arg1 ; ... }
>   f ("Hello", 42);
> 
> (There's some caveat with arrays in the function argument list.)
> 
> Janis

Not sure why that would be, can you offer more detail?

At any rate, as always thanks for the brainfood.

-- 
:wq
Mike Sanders

[toc] | [prev] | [next] | [standalone]


#9819

FromJanis Papanagnou <janis_papanagnou+ng@hotmail.com>
Date2024-08-22 00:51 +0200
Message-ID<va5r13$1id6$1@dont-email.me>
In reply to#9815
On 21.08.2024 20:57, Mike Sanders wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> [...]
>>
>> (There's some caveat with arrays in the function argument list.)
> 
> Not sure why that would be, can you offer more detail?

Assuming you meant the statement in parenthesis... - Most prominent
one is the difference in parameter passing depending on the parameter
type; arrays are passed by reference, scalars by value. So any change
of array elements inside the function will change the array object on
the caller's side.

(There was also a case where awk got an issue in differentiating an
array from a scalar in functions, but details evade my memory at the
moment.)

Janis

[toc] | [prev] | [next] | [standalone]


#9814

FromBen Bacarisse <ben@bsb.me.uk>
Date2024-08-21 16:38 +0100
Message-ID<87v7zt28f8.fsf@bsb.me.uk>
In reply to#9811
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.

[toc] | [prev] | [next] | [standalone]


#9816

Fromporkchop@invalid.foo (Mike Sanders)
Date2024-08-21 19:01 +0000
Message-ID<va5di7$3vg5l$2@dont-email.me>
In reply to#9814
Ben Bacarisse <ben@bsb.me.uk> wrote:

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

Have seen this elsewhere too.

-- 
:wq
Mike Sanders

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.awk


csiph-web