Home > Archive > AWK > October 2006 > "call" extension
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Peter V. Saveliev 2006-10-22, 6:56 pm |
| ....
Hello! I just finished another extension that can be interesting also
outside of xgawk community:
http://xgawk.radlinux.org/Articles/extension-call/show
It can be used e.g to avoid constructions like this:
if (var ~ /func1/) {
func1(...)
} else if (var ~ /func2/) {
func2(...)
} else ...
and use instead:
if (var ~ /func[12...]/) {
call(var,...)
}
or smtng like this, when you want to call a function by input (after
some validation, surely :))
After "call" extension, there can be another step -- eval(), and I
think, which caveats there
can be.
| |
| Jürgen Kahrs 2006-10-22, 6:56 pm |
| Peter V. Saveliev wrote:
> if (var ~ /func[12...]/) {
> call(var,...)
> }
>
> or smtng like this, when you want to call a function by input (after
> some validation, surely :))
This feature could be named "call by name".
The name of the function to be invoked is
not known when the interpreter reads the
script. The name of the function is only
determined later at run-time of the script.
| |
| Kenny McCormack 2006-10-23, 7:55 am |
| In article <1161527180.674031.62090@m73g2000cwd.googlegroups.com>,
Peter V. Saveliev <peet@peet.spb.ru> wrote:
>...
>
>Hello! I just finished another extension that can be interesting also
>outside of xgawk community:
>
>http://xgawk.radlinux.org/Articles/extension-call/show
>
>It can be used e.g to avoid constructions like this:
>
>if (var ~ /func1/) {
> func1(...)
>} else if (var ~ /func2/) {
> func2(...)
>} else ...
>
>and use instead:
>
>if (var ~ /func[12...]/) {
> call(var,...)
>}
>
>or smtng like this, when you want to call a function by input (after
>some validation, surely :))
TAWK has this. It is nice in theory, but, FWIW, I've only had the need
to use it a few times (in many years of TAWK programming).
Specifically, I've never actually had to call a function "through" a
variable. My guess is that a large percentage of the time this
functionality is requested, it is not needed - i.e., some newbie just
thinks it is. Unless you are doing something really complex or writing
a debugger, I don't see the need for it.
Given the above, you might wonder why call is ever used (if not to call
a function through a variable). I won't go into details, but suffice to
say that there are other reasons (at least in TAWK's implementation)
>After "call" extension, there can be another step -- eval(), and I
>think, which caveats there
>can be.
TAWK does not have eval; I once requested it and was told it would be
very difficult. My guess is that it would have efficiency implications.
Note that one of the very many nice things about TAWK is that it is very
fast (as AWKs go). I like that.
If you are looking for other TAWK-ish things to implement in GAWK (given
that TAWK is hard to find these days, I applaud the effort to evolve
GAWK into TAWK, to whatever extent is possible), I'd say my #1 would be
real multi-dimensional arrays. A real lifesaver it is.
| |
| Andrew Schorr 2006-10-23, 6:56 pm |
| Kenny McCormack wrote:
> In article <1161527180.674031.62090@m73g2000cwd.googlegroups.com>,
> Peter V. Saveliev <peet@peet.spb.ru> wrote:
[color=darkred]
> TAWK has this. It is nice in theory, but, FWIW, I've only had the need
> to use it a few times (in many years of TAWK programming).
What syntax does TAWK use for this? It would be nice to be
compatible...
Also, for multidimensional arrays, what syntax does TAWK provide?
Although I suspect this would be a fair bit of work to integrate into
gawk...
Regards,
Andy
| |
| news.t-online.de 2006-10-23, 6:56 pm |
| Andrew Schorr wrote:
> Kenny McCormack wrote:
>
>
>
>
>
> What syntax does TAWK use for this? It would be nice to be
> compatible...
>
> Also, for multidimensional arrays, what syntax does TAWK provide?
> Although I suspect this would be a fair bit of work to integrate into
> gawk...
>
> Regards,
> Andy
>
http://www.unix.org.ua/orelly/unix/sedawk/ch11_03.htm
| |
| Kenny McCormack 2006-10-23, 6:56 pm |
| In article <1161626596.449920.15310@m73g2000cwd.googlegroups.com>,
Andrew Schorr <aschorr@telemetry-investments.com> wrote:
>Kenny McCormack wrote:
>
>
>What syntax does TAWK use for this? It would be nice to be
>compatible...
call(funname,arguments, ...)
and
calla(funname,argument_array)
examples:
call("print","hello, world")
split("hello, world",x)
calla("print",x)
Note that here "print" can be thought of as a built-in function, even
though it doesn't normally parse that way:
print print "hello, world"
is a syntax error, but:
print call("print","hello, world")
is OK.
As I say, it hasn't been all that useful for me - TAWK already supports
"variadic" user-defined-functions, so even though you might think that
the calla() function would be neat for that reason, it isn't needed in
practice.
Example (the purpose here is to avoid a runtime warning):
# This function uses TAWK extensions: argval() and argcount()
function max() {
local i,max = argval(1),ac = argcount()
for (i=2; i<=ac; i++)
if (argval(i) > max)
max = argval(i)
return max
}
Now, we would like to be able to call max with any number of args
(at least 1). But we want to be able to define it "generically", i.e.,
without any indication in the definition as to how many args will be
supplied at runtime.
The problem is that if we call it as, say:
x = max(1,2,3)
the compiler will notice that it was declared to take no args and warn
us about unused args. So, instead, we do:
x = call("max",1,2,3)
and we are able to hide the fact that we are calling a nominally
arg-less function with args.
>Also, for multidimensional arrays, what syntax does TAWK provide?
>Although I suspect this would be a fair bit of work to integrate into
>gawk...
The URL the next poster provides covers it pretty well, but it boils
down to:
x[1][2] = "test"
for (i in x)
for (j in x[i])
print i,j,x[i][j]
Note that technically, it is not multidimensional arrays, but rather
arrays of arrays (which in my opinion is much better anyway).
| |
| Andrew Schorr 2006-10-24, 7:56 am |
| Kenny McCormack wrote:
> The URL the next poster provides covers it pretty well, but it boils
> down to:
I'm afraid that URL didn't work for me, not sure why...
> x[1][2] = "test"
> for (i in x)
> for (j in x[i])
> print i,j,x[i][j]
>
> Note that technically, it is not multidimensional arrays, but rather
> arrays of arrays (which in my opinion is much better anyway).
OK, that's what I expected. Is there any limit on the number
of dimensions? I would assume that there isn't...
Regards,
Andy
| |
| Kenny McCormack 2006-10-30, 7:01 pm |
| In article <1161698093.590283.296890@b28g2000cwb.googlegroups.com>,
Andrew Schorr <aschorr@telemetry-investments.com> wrote:
>Kenny McCormack wrote:
>
>I'm afraid that URL didn't work for me, not sure why...
>
>
>OK, that's what I expected. Is there any limit on the number
>of dimensions? I would assume that there isn't...
No definite limit - obviously it is not infinite - the manual talks
about the practical limits on array size based on OS, amount of memory,
etc. As you can imagine, the limits are not that high under DOS, but
more or less unlimited on modern (32 bit) OSs.
The most I've ever used is 4.
| |
| John DuBois 2006-10-30, 7:01 pm |
| In article <ehig7q$33i$2@news.xmission.com>,
Kenny McCormack <gazelle@xmission.xmission.com> wrote:
>Specifically, I've never actually had to call a function "through" a
>variable. My guess is that a large percentage of the time this
>functionality is requested, it is not needed - i.e., some newbie just
>thinks it is. Unless you are doing something really complex or writing
>a debugger, I don't see the need for it.
I've certainly wished for such a capability a few times over the years. Not
for the nicety of avoiding a multilevel if/then - rather, so that a function
name could be passed to another function (as for qsort; also error handlers,
libraries for processing generalized data structures, and such). Maybe this
falls under "really complex" :) It'd also be nice if such a capability had a
means for uniquely indicating the error "no function with that name"; then it
could be used for such tests as "is mktime() available? If not fall back to
this..."
>I'd say my #1 would be real multi-dimensional arrays. A real lifesaver it is.
Yeah. Just yesterday I had to once again use a function I wrote to painfully
take a slice from a (pseudo-)multidimensional array...
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
| |
| Jürgen Kahrs 2006-10-30, 7:01 pm |
| John DuBois wrote:
> I've certainly wished for such a capability a few times over the years. Not
> for the nicety of avoiding a multilevel if/then - rather, so that a function
> name could be passed to another function (as for qsort; also error handlers,
That's what I meant when I described this as "call by name".
|
|
|
|
|