在 zsh 中,如何将匿名数组传递给函数?

发布于 2024-07-12 07:08:44 字数 195 浏览 4 评论 0原文

在 zsh 中,如何将匿名数组传递给函数? 例如寻找类似的东西:

foo() {
  echo ${1[2]} '\n';
}

a=(abc def ghi)
foo $a

--> def

或者理想情况下:

foo (abc def ghi)

In zsh, how do I pass anonymous arrays into functions? e.g. looking for something like:

foo() {
  echo ${1[2]} '\n';
}

a=(abc def ghi)
foo $a

--> def

Or ideally:

foo (abc def ghi)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

澜川若宁 2024-07-19 07:08:44

您可以将数组的名称传递给函数,然后函数可以通过将名称解释为变量名称来读取数组。 这种技术对于 bash 中的关联数组等也很有用。 ZSH 使其变得非常容易,因为 (P) 运算符将以所需的方式解释变量。

一个例子可以清楚地说明这一点。 如果您定义此函数:

function i_want_array () {
    local array_name=$1

    echo "first array element is: " ${(P)${array_name}[1]}
}

那么以下代码将执行它:

% a=(one two three)
% i_want_array "a"
first array element is:  one

澄清一下,这是通过对真实数组进行操作来实现的,因此任何空白都会被正确处理:

% a=("one two" three)
% i_want_array "a"
first array element is:  one two

You can pass the name of the array to the function and then the function can read the array by interpreting the name as a variable name. It is a technique that is also useful for things like associative arrays in bash. ZSH makes it very easy to do, as the (P) operator will interpret the variable in the desired way.

An example will make this clear. If you define this function:

function i_want_array () {
    local array_name=$1

    echo "first array element is: " ${(P)${array_name}[1]}
}

Then the following code will execute it:

% a=(one two three)
% i_want_array "a"
first array element is:  one

And just to clarify, this works by operating on the real array, so any whitespace is handled correctly:

% a=("one two" three)
% i_want_array "a"
first array element is:  one two
゛时过境迁 2024-07-19 07:08:44

你不能。 函数像任何其他命令一样采用位置参数。

另请注意,您的解决方法不允许任何“数组”元素包含空格。

我能想到的最干净的事情是要求调用者创建一个本地数组,然后从函数中读取它:

$ foo() {
   for element in $FOO_ARRAY
   do
       echo "[$element]"
   done
}
$ local FOO_ARRAY; FOO_ARRAY=(foo bar baz quux)
$ foo
[foo]
[bar]
[baz]
[quux]

我知道 bash 为其补全系统做了类似的杂技,我认为 zsh 也可能如此。 这并不是太不寻常。

You can't. Functions take positional parameters like any other command.

Note also that your workaround doesn't allow any of the "array" elements to contain whitespace.

The cleanest thing I can think of is to require that the caller create a local array, then read it from the function:

$ foo() {
   for element in $FOO_ARRAY
   do
       echo "[$element]"
   done
}
$ local FOO_ARRAY; FOO_ARRAY=(foo bar baz quux)
$ foo
[foo]
[bar]
[baz]
[quux]

I know bash does similar acrobatics for its completion system, and I think zsh might, too. It's not too unusual.

满栀 2024-07-19 07:08:44

如果您只需要一个数组参数:作为 tail args。

foo() {
  : if you have any leading non-array args, grab those first:
  local arg1="$1"
  shift
  local arg2="$1"
  shift
  : now $@ is your array arg
  echo $@[2] '\n';
}

a=(abc def ghi)
foo "arg 1" arg2 $a

--> def

If you only need one array parameter: as tail args.

foo() {
  : if you have any leading non-array args, grab those first:
  local arg1="$1"
  shift
  local arg2="$1"
  shift
  : now $@ is your array arg
  echo $@[2] '\n';
}

a=(abc def ghi)
foo "arg 1" arg2 $a

--> def
你的他你的她 2024-07-19 07:08:44

没有用匿名数组解决......但我尝试了这个东西! 猛击!!...

function join {
  local tmp=($1)

  for (( i=0 ; i<${#tmp[@]}-1 ; i++ )) ; do
    echo -n ${tmp[$i]}$2
  done

  echo ${tmp[$i]}
}

test="abc def ghi"

join "$test" "|"

Not solved with an anonymous array ... But i tried this thing in !! BASH !!...

function join {
  local tmp=($1)

  for (( i=0 ; i<${#tmp[@]}-1 ; i++ )) ; do
    echo -n ${tmp[$i]}$2
  done

  echo ${tmp[$i]}
}

test="abc def ghi"

join "$test" "|"
孤寂小茶 2024-07-19 07:08:44

找到了一个解决方法:

foo() {
  local a=$1
  local b=$2

  echo ${(j:---:)${=b}}

  foreach d in ${${=b}}
  do
      echo $d
  done
}

其中parameter2是一串白色分隔的文本,例如'a badkljf odod'

Figured out a workaround:

foo() {
  local a=$1
  local b=$2

  echo ${(j:---:)${=b}}

  foreach d in ${${=b}}
  do
      echo $d
  done
}

Where parameter2 is a string of white-separated text, e.g. 'a badkljf odod'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文