|
|
Local array variables in functions
Array parameters passed to functions are passed 'by reference'
in awk, and changing an array inside the function will change
its origin outside the function as well. How about arrays that
are defined as local variables in the argument list? In GNU awk,
as one may expect, arrays are initialized to an empty array.[*]
Is that generally guaranteed with other awks as well? (POSIX?)
Another array thing that puzzled me (this is GNU Awk 3.1.6)...
BEGIN { global_array[2] ; some_array[1] }
{ global_array[3] }
{ f(some_array) }
function f(param_array, local_array)
{
print length(local_array)
print length(global_array)
print length(some_array)
print length(param_array) # this line leads to a runtime error
}
fatal: attempt to use array `param_array (from some_array)' in a
scalar context
Thanks for your comments.
Janis
[*] I was uncertain about that because of the reference character
of arrays declared in the function parameter list, especially in
case the respective function will be called more than once.
|
|
0
|
|
|
|
Reply
|
Janis
|
8/26/2009 9:37:59 PM |
|
Janis Papanagnou wrote:
> Array parameters passed to functions are passed 'by reference'
> in awk, and changing an array inside the function will change
> its origin outside the function as well. How about arrays that
> are defined as local variables in the argument list? In GNU awk,
> as one may expect, arrays are initialized to an empty array.[*]
> Is that generally guaranteed with other awks as well? (POSIX?)
"The number of parameters in the function definition need not match the
number of parameters in the function call. Excess formal parameters can be
used as local variables. If fewer arguments are supplied in a function call
than are in the function definition, the extra parameters that are used in
the function body as scalars shall evaluate to the uninitialized value
until they are otherwise initialized, and the extra parameters that are
used in the function body as arrays shall be treated as uninitialized
arrays where each element evaluates to the uninitialized value until
otherwise initialized."
This seems to imply that awk has to do a compile time check of the code in
the function body, to see how it uses the (extra) parameters, so it can
correctly initialize them. In practice I think it can do away with that and
just wait until the parameter is first used, and act accordingly.
I'm sure I'll be corrected if I'm saying something wrong.
> Another array thing that puzzled me (this is GNU Awk 3.1.6)...
>
> BEGIN { global_array[2] ; some_array[1] }
>
> { global_array[3] }
> { f(some_array) }
>
> function f(param_array, local_array)
> {
> print length(local_array)
> print length(global_array)
> print length(some_array)
> print length(param_array) # this line leads to a runtime error
> }
>
> fatal: attempt to use array `param_array (from some_array)' in a
> scalar context
That was most likely a bug in 3.1.6 (I can reproduce it too), but with
version 3.1.7 it seems to have been fixed, because with the above code I
correctly get
0
2
1
1
as output, with no errors.
|
|
0
|
|
|
|
Reply
|
pk
|
8/26/2009 10:50:27 PM
|
|
"pk" <pk@pk.invalid> ?????? ?????? news:h74ecn$93l$1@aioe.org...
> Janis Papanagnou wrote:
>
>> Array parameters passed to functions are passed 'by reference'
>> in awk, and changing an array inside the function will change
>> its origin outside the function as well. How about arrays that
>> are defined as local variables in the argument list? In GNU awk,
>> as one may expect, arrays are initialized to an empty array.[*]
>> Is that generally guaranteed with other awks as well? (POSIX?)
>
> "The number of parameters in the function definition need not match the
> number of parameters in the function call. Excess formal parameters can be
> used as local variables. If fewer arguments are supplied in a function
> call
> than are in the function definition, the extra parameters that are used in
> the function body as scalars shall evaluate to the uninitialized value
> until they are otherwise initialized, and the extra parameters that are
> used in the function body as arrays shall be treated as uninitialized
> arrays where each element evaluates to the uninitialized value until
> otherwise initialized."
>
> This seems to imply that awk has to do a compile time check of the code in
> the function body, to see how it uses the (extra) parameters, so it can
> correctly initialize them. In practice I think it can do away with that
> and
> just wait until the parameter is first used, and act accordingly.
> I'm sure I'll be corrected if I'm saying something wrong.
>
>> Another array thing that puzzled me (this is GNU Awk 3.1.6)...
>>
>> BEGIN { global_array[2] ; some_array[1] }
>>
>> { global_array[3] }
>> { f(some_array) }
>>
>> function f(param_array, local_array)
>> {
>> print length(local_array)
>> print length(global_array)
>> print length(some_array)
>> print length(param_array) # this line leads to a runtime error
>> }
>>
>> fatal: attempt to use array `param_array (from some_array)' in a
>> scalar context
>
> That was most likely a bug in 3.1.6 (I can reproduce it too), but with
> version 3.1.7 it seems to have been fixed, because with the above code I
> correctly get
>
> 0
> 2
> 1
> 1
>
> as output, with no errors.
>
http://cvs.savannah.gnu.org/viewvc/gawk-stable/ChangeLog?root=gawk&view=markup
Sat Mar 15 22:17:21 2008 Arnold D. Robbins <arnold@skeeve.com>
* builtin.c (do_length): Handle the case of the parameter being
an array that was a function parameter.
|
|
0
|
|
|
|
Reply
|
Rajan
|
8/29/2009 3:55:36 AM
|
|
|
2 Replies
329 Views
(page loaded in 0.111 seconds)
|
|
|
|
|
|
|
|
|