[英]AWK Curry Function - Can a function return a function?
I'm working on an AWK script that quite a big mess at the moment, and I'm trying to improve it (primarily because I want to improve my Awk scripting skillz) 我目前正在处理一个相当混乱的AWK脚本,并且我正在尝试对其进行改进(主要是因为我想提高自己的Awk脚本技术)
I realize that there isn't a way to do Object Oriented Programming within Awk or Gawk, but is there a way to at least curry functions? 我意识到没有办法在Awk或Gawk中进行面向对象的编程,但是至少有咖喱函数可以吗? As in returning a function from within a function?
就像从函数内部返回函数一样? (Not return the result of the executed function, but rather return a function that can be executed)
(不返回执行函数的结果,而是返回可以执行的函数)
I found a Stack Overflow post , where @GreenFox showed that its possible to execute a function with the name of the function stored in a variable. 我发现了一个Stack Overflow帖子 ,其中@GreenFox表明可以使用存储在变量中的函数名称执行函数。 The example he posted is below:
他发布的示例如下:
function foo(s){print "Called foo "s}
function bar(s){print "Called bar "s}
{
var = "";
if(today_i_feel_like_calling_foo){
var = "foo";
}else{
var = "bar";
}
@var( "arg" ); # This calls function foo(), or function bar() with "arg"
}
What I'm wondering is if its possible to return a function from another function. 我想知道的是是否有可能从另一个函数返回一个函数。
For example, a function that accepts a string that can be used in awks printf
as a format, and returns a function that accepts two other arguments, and essentially executes printf( fmt_from_parent_func, sub_func_arg1, sub_func_arg2 )
. 例如,一个函数接受可以在awks
printf
用作格式的字符串,然后返回一个接受其他两个参数的函数,并实质上执行printf( fmt_from_parent_func, sub_func_arg1, sub_func_arg2 )
。
Here's my attempt at trying to accomplish the following: 这是我尝试完成以下任务的尝试:
#! /usr/local/bin/awk -f
function setFmt ( fmt ){
function _print ( var, val ){
printf ( fmt ? fmt : "%-15s: %s\n" ), str1, str2
}
return @_print
}
BEGIN {
fmtA = setFmt("%-5s: %s\n")
@fmtA("ONE","TWO")
}
Which results in the errors: 导致错误:
awk: ./curry.awk:4: function _print ( var, val ){
awk: ./curry.awk:4: ^ syntax error
awk: ./curry.awk:4: function _print ( var, val ){
awk: ./curry.awk:4: ^ syntax error
awk: ./curry.awk:6: printf ( fmt ? fmt : "%-15s: %s\n" ), str1, str2
awk: ./curry.awk:6: ^ unexpected newline or end of string
awk: ./curry.awk:11: fmtA = setFmt("%-5s: %s\n")
awk: ./curry.awk:11: ^ unexpected newline or end of string
awk: ./curry.awk:12: @fmtA("ONE","TWO")
awk: ./curry.awk:12: ^ unexpected newline or end of string
If anyone knows if this is at all possible (which Im starting to see myself), and knows a way to accomplish something to this effect.. that would be awesome. 如果有人知道这是完全有可能的(林先生开始看到我自己),并且知道一种达到这种效果的方法。那真是太棒了。
Thanks! 谢谢!
With GNU awk you can return the name of a function as a string from another function but you can't declare a function within another function nor can you return a function (or an array) in any awk - all you can return from a function in awk is a scalar value (ie a number or string). 使用GNU awk,您可以将函数的名称作为字符串从另一个函数返回,但是您不能在另一个函数中声明一个函数,也不能在任何awk中返回一个函数(或数组)-您可以从一个函数返回的所有内容awk中的是标量值(即数字或字符串)。
Is this what you're trying to do: 这是您要执行的操作:
$ cat tst.awk
function _print ( var, val ){
printf _print_fmt, var, val
}
function setFmt ( fmt ){
_print_fmt = (fmt ? fmt : "%-15s: %s\n" )
return "_print"
}
BEGIN {
fmtA = setFmt("%-5s: %s\n")
@fmtA("ONE","TWO")
}
$ awk -f tst.awk
ONE : TWO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.