Passing array as argument to function 
Author Message
 Passing array as argument to function

function load(array) {
 for (i=1; i<=10; i++) array[i]=i
Quote:
}

BEGIN {
 load(myarray)
 delete myarray
Quote:
}

{
 exit
Quote:
}

END {
 for (i in myarray) print myarray[i]

Quote:
}

As expected, this prints nothing.

The point is: I am passing the 'myarray' as argument to the function load()
which loads the array. Since arrays are passed by reference, this works.

But, in a larger program, I tried this sort of thing and got

'Attempt to use variable 'array' as a array' on my "delete array" statement.

What is going on? At the point the function is called, AWK has no way of
knowing if this is a regular variable or a array until the function actually
executes, right? So, I kind of agree with the error message, but then how
did it work in my trivial example above?

Or do I need to a dummy 'array[""]=1' just to nudge AWK into thinking that
this is a array, not a variable.

Thanks



Fri, 29 Oct 2004 21:35:55 GMT  
 Passing array as argument to function
Hallo,

Quote:

> But, in a larger program, I tried this sort of thing and got

> 'Attempt to use variable 'array' as a array' on my "delete array" statement.

> What is going on?

who can guess?  You should send your program.
It always helps to try to shorten your program as much as possible, taking
care that your bug doesn't disappear on the way.

Quote:
> Or do I need to a dummy 'array[""]=1' just to nudge AWK into thinking that
> this is a array, not a variable.

Inserting this in a BEGIN block at the top of the program may help.
Perhaps awk will tell you

        Attempt to use array "x" as a scalar

and you'll find the source of your bug.

HTH,
        Stepan Kasal



Fri, 29 Oct 2004 22:32:51 GMT  
 Passing array as argument to function

<snip working code>

Quote:

> But, in a larger program, I tried this sort of thing and got

> 'Attempt to use variable 'array' as a array' on my "delete array"
statement.

> What is going on? At the point the function is called, AWK has no way
of
> knowing if this is a regular variable or a array until the function
actually
> executes, right? So, I kind of agree with the error message, but then
how
> did it work in my trivial example above?

> Or do I need to a dummy 'array[""]=1' just to nudge AWK into thinking
that
> this is a array, not a variable.

> Thanks

I guess that you have used the name "array" for a non-array (scalar)
variable name sometime before your function that uses "array" as an
array variable name is called.  I can't see how you would get this error
otherwise.

The point is that once an array variable, always an array variable in
awk.  And, of course, once a scalar variable, always a scalar variable.

To my mind there's no harm in giving awk a hint as you suggest, provided
that you remember to get rid of that element later.

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Sat, 30 Oct 2004 04:23:43 GMT  
 Passing array as argument to function


...

Quote:
>I guess that you have used the name "array" for a non-array (scalar)
>variable name sometime before your function that uses "array" as an
>array variable name is called.  I can't see how you would get this error
>otherwise.

>The point is that once an array variable, always an array variable in
>awk.  And, of course, once a scalar variable, always a scalar variable.

You sure about that?

 % tawk 'BEGIN{print typeof(t);split("foo bar",t);print typeof(t);t = "hi";print typeof(t)}'
 uninitialized
 array
 string
 %



Sat, 30 Oct 2004 04:28:59 GMT  
 Passing array as argument to function

Quote:


>...
>>The point is that once an array variable, always an array variable in
>>awk.  And, of course, once a scalar variable, always a scalar variable.

>You sure about that?

>% tawk 'BEGIN{print typeof(t);split("foo bar",t);print typeof(t);
>t ="hi";print typeof(t)}'
>uninitialized
>array
>string
>%

How odd. I get

$ gawk 'BEGIN { print typeof(t) }'
gawk: cmd. line:1: fatal: function `typeof' not defined
$

Oh, that's right. You're using that perl-wannabe dialect, aren't you?

For the lurkers: as a general rule it's a good idea to ignore Kenny. In this
particular case, neither the typeof function nor the ability to assign a scalar
then an array or vice versa is generally available in awk. There are even a few
of us who would think the ability to blast an array into oblivion with a single
unremarked scalar assignment would make tawk rather a difficult dialect to
debug. Seems like a profoundly blockheaded piece of syntax to me.



Sat, 30 Oct 2004 12:19:19 GMT  
 Passing array as argument to function
Stephan is right: show us some more critical code.

Besides this, please declare the function-internal variable "i" as local

Quote:
> function load(array) {
>  for (i=1; i<=10; i++) array[i]=i
> }

e.g. this way

function load(array,   i) {
..........

This error often leads to unbelievable errors.

Jurgen



Sat, 30 Oct 2004 13:56:30 GMT  
 Passing array as argument to function

Quote:


> >...
> >>The point is that once an array variable, always an array variable
in
> >>awk.  And, of course, once a scalar variable, always a scalar
variable.

> >You sure about that?

> >% tawk 'BEGIN{print typeof(t);split("foo bar",t);print typeof(t);
> >t ="hi";print typeof(t)}'
> >uninitialized
> >array
> >string
> >%

> How odd. I get

> $ gawk 'BEGIN { print typeof(t) }'
> gawk: cmd. line:1: fatal: function `typeof' not defined
> $

> Oh, that's right. You're using that perl-wannabe dialect, aren't you?

> For the lurkers: as a general rule it's a good idea to ignore Kenny.
In this
> particular case, neither the typeof function nor the ability to assign
a scalar
> then an array or vice versa is generally available in awk. There are
even a few
> of us who would think the ability to blast an array into oblivion with
a single
> unremarked scalar assignment would make tawk rather a difficult
dialect to
> debug. Seems like a profoundly blockheaded piece of syntax to me.

This is, I think the least attractive of tawk's features.  If you are
going to allow some form of typeof capability then the obvious thing to
do is to introduce name spaces for scalars and arrays.  I have had some
email discussions with Arnold about name spaces for awk, but the
introduction of them would probably cause old, poorly thought out
programs to break.  A further problem would be that too many source code
changes would be needed for most implementations of awk to make this a
worthwhile addition.

You would also probably get into the need to differentiate between
numeric and string scalars and then you arrive at a new "perl-wannabe".

My 0.02p,
Peter
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Mon, 15 Nov 2004 13:51:03 GMT  
 Passing array as argument to function


% do is to introduce name spaces for scalars and arrays.  I have had some
% email discussions with Arnold about name spaces for awk, but the
% introduction of them would probably cause old, poorly thought out
% programs to break.

Possibly more useful would be the concept of local variables, as in
explicitly declared variables which are local to a brace-delimited
block. A variable which was declared local to a block would have
precisely the same characteristics as an unpassed function argument,
so this wouldn't be introducing new capabilities to the language,
or even changing it radically, but it would avoid some of the
clashes you might hope to avoid through namespaces.

--

Patrick TJ McPhee
East York  Canada



Mon, 22 Nov 2004 10:04:33 GMT  
 Passing array as argument to function


Quote:


> % do is to introduce name spaces for scalars and arrays.  I have had
some
> % email discussions with Arnold about name spaces for awk, but the
> % introduction of them would probably cause old, poorly thought out
> % programs to break.

> Possibly more useful would be the concept of local variables, as in
> explicitly declared variables which are local to a brace-delimited
> block. A variable which was declared local to a block would have
> precisely the same characteristics as an unpassed function argument,
> so this wouldn't be introducing new capabilities to the language,
> or even changing it radically, but it would avoid some of the
> clashes you might hope to avoid through namespaces.

> --

> Patrick TJ McPhee
> East York  Canada


Well, yes, locally declared variables would be nice (although it would
make awk a bit too much like perl for my liking).  Once again though
this could cause too many old programs to break, unless steps were taken
so that an awk-style locally declared variable in the function parameter
list were handled properly, i.e., in the same way as an explicitly
declared local variable - as you say.

The issue of namespaces isn't really about scoping, it's about a
variable's "declared" characteristics, i.e., is it an array variable or
a scalar?  I wouldn't want awk to have to go down the perl/etc. route of

whatever" family of decorators is one of the things that I _really_
dislike about perl.  I don't need decorators to tell me if a variable is
a scalar or not - especially if I'm any use as a programmer.

Peter
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Mon, 22 Nov 2004 12:51:18 GMT  
 Passing array as argument to function

Quote:

> This is, I think the least attractive of tawk's features.  If you are
> going to allow some form of typeof capability then the obvious thing to
> do is to introduce name spaces for scalars and arrays.  I have had some
> email discussions with Arnold about name spaces for awk, but the
> introduction of them would probably cause old, poorly thought out
> programs to break.  A further problem would be that too many source code
> changes would be needed for most implementations of awk to make this a
> worthwhile addition.

> You would also probably get into the need to differentiate between
> numeric and string scalars and then you arrive at a new "perl-wannabe".

> My 0.02p,
> Peter

So there is very little chance I guess for an addition to awk in the
future along the lines say of tcls upvar? That would be nice IMHO.


Mon, 22 Nov 2004 14:58:42 GMT  
 Passing array as argument to function


% Well, yes, locally declared variables would be nice (although it would
% make awk a bit too much like perl for my liking).

I was thinking there would be only one way to decalare them, it would be
obvious, and do what you expect, though. Not like perl at all.

--

Patrick TJ McPhee
East York  Canada



Tue, 23 Nov 2004 11:47:35 GMT  
 Passing array as argument to function


Quote:


> % Well, yes, locally declared variables would be nice (although it
would
> % make awk a bit too much like perl for my liking).

> I was thinking there would be only one way to decalare them, it would
be
> obvious, and do what you expect, though. Not like perl at all.

If there's only one way to declare them then I agree that would be
sensible, but you'd need to differentiate arrays and scalars in the
declaration(s) to make it worth while, something like:

function awkfun( a, b,        x, y[]) {
    local m[], n[], p
    ...

Quote:
}

I guess.

Is this the sort of thing that you're thinking of?

Regards,
Peter
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Tue, 23 Nov 2004 14:17:11 GMT  
 Passing array as argument to function


Quote:


>% Well, yes, locally declared variables would be nice (although it would
>% make awk a bit too much like perl for my liking).

>I was thinking there would be only one way to decalare them, it would be
>obvious, and do what you expect, though. Not like perl at all.

I.e., like T-AWK.  T-AWK has a nice, normal syntax for local variables.

But of course.  That's what you come to expect from a modern, professional
AWK implementation (as opposed to a hobby implementation).



Tue, 23 Nov 2004 20:40:18 GMT  
 Passing array as argument to function


% If there's only one way to declare them then I agree that would be
% sensible, but you'd need to differentiate arrays and scalars in the
% declaration(s) to make it worth while, something like:
%
% function awkfun( a, b,        x, y[]) {
%     local m[], n[], p
%     ...
% }

Not really. Mostly, I was taking a dig at local variables in perl. The
local keyword there declares what we call in lisp `special variables',
a sort-of stack-constrained global. I think this was done by accident,
and the non-obvious keyword `my' was introduced to create a lexical
local variable.

Anyway, I don't see the benefit of distinguishing between arrays and
scalars, provided you can constrain the scope of the variable name.
As with unused function parameters, the local can be anything:

  function f(a,b,  c, d, i)
  {
    c = a*b
    for (i = a; i <= b; i++)
      d[i] = c * i + d[i-1]
    return d[b]  
  }

that will work today, but suppose you wanted to add some isolation to
an action:

  {
    local c,d,i
    c = $1*$2
    for (i = $1; i <= $2; i++)
      d[i] = c * i + d[i-1]

    print d[b]  
  }

This would reduce problems if you're including libraries -- you wouldn't
have a conflict with a global called d from some file included using -f.

There's always something you could change, of course, and the danger of
constantly including new features is that you end up ruining the language.
My other big idea was to add a cond statement.

 cond {
   pattern action
   pattern action
   pattern action
   pattern action
 }

this would allow a pattern/action loop to be stuck in the middle of
a function or some other kind of block.

Somewhere between those two ideas, the slope started getting pretty
slippery.
--

Patrick TJ McPhee
East York  Canada



Thu, 25 Nov 2004 03:25:19 GMT  
 Passing array as argument to function

Quote:


>I.e., like T-AWK.  T-AWK has a nice, normal syntax for local variables.

>But of course.  That's what you come to expect from a modern, professional
>AWK implementation (as opposed to a hobby implementation).

I support several orders of magnitude more customers than Mr. Thompson
does,[1] in what may be several orders of magnitude less time spent
doing it.  Gawk runs on everything from '386s up to Crays.  And if
you find a bug in tawk, is there any chance of your fixing it yourself?

Perhaps you should put brain in gear before engaging fingers and think
about who you're insulting when you post.

[1] EVERY Linux system that I know of, lots or all of the *BSD systems,
and tons of people using gawk on commercial *nix.  I figure there are
literally millions of systems using gawk on a day-to-day basis.
Not too shabby for a "hobby implementation."
--

P.O. Box 354            Home Phone: +972  8 979-0381    Fax: +1 928 569 9018
Nof Ayalon              Cell Phone: +972 51  297-545
D.N. Shimshon 99785     ISRAEL



Sun, 05 Dec 2004 21:22:06 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Passing array valued functions as argument to function.

2. passing functions with variable number of arguments as procedure arguments

3. F77-C: Passing Fortran FUNCTION as an argument for a C Function

4. F95: array-returning function as argument to another function

5. fixed length character array passed as a real argument

6. IFC6.0 Array Sections and Argument Passing

7. IFC Argument passing and array subsections

8. passing array arguments to subroutines

9. Passing of allocated array as dummy argument

10. pass array as argument

11. Passing allocatable arrays as a assumed shape argument?

12. Possible to pass entire array as argument?

 

 
Powered by phpBB® Forum Software