用空格循环数组字符串 [英] Loop through array of arrays of string with spaces

查看:171
本文介绍了用空格循环数组字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图循环一个包含其他数组的数组,这些数组由包含空格的字符串组成。问题是,我似乎无法保留字符串中的间距。如果我将IFS更改为\\\
,或者将数组的所有元素都视为1项,如果我将IFS保持不变,则这些字符串会被分成多个项目:

 #!/ bin / sh 
low1 =(AA QQBB LL)
low2 =(CCDD)
low3 =(EEFF)
high =(low1 low2 low3)

for high_item in $ {high [@]}
do
eval arrayz = \ $ {$ high_item [@]}
#IFS = $'\\\
'
for $ arrayz

echo $ item
完成
完成

输出:

 
AA
QQ
BB
LL
CC
DD
EE
FF

您可以看到元素AA QQ和BB LL已被分割。



如果我取消注释将 IFS 设置为 \\\
的行,我得到以下结果:

 
AA QQ BB LL
CC DD
EE FF

现在将AA QQ和BB LL连接在一起!



有没有反正我可以保留这些元素,就像他们原来的一样...我需要的输出看起来像这样:

 
AA QQ
BB LL
CC
DD
EE
FF


解决方案

我认为你的意思是输出应该是这样的:

  AA QQ 
BB LL
CC
DD
EE
FF



  $ {low1 [0]} 
$ {low1 [1]}
$ {low2 [0]}
$ {low2 [1]}
$ {low3 [0]}
$ {low3 [1]}

可以使用以下方式来完成:

 #!/ bin / bash 

low1 =(AA QQBB LL)
low2 =(CCDD)
low3 =(EEFF)
high =(low1 low2 low3)

为high_item in $ {high [@]}
do
x = $ {high_item} [@]# - > $ {array [']
arrays =($ {!x})

#IFS = $'\ n'
@]}
do
echo$ item
done
done

总是使用#!/ bin / bash 作为bash脚本。 b $ b

解释: $ {!x} 是间接变量扩展。它的计算结果是一个包含在 $ x 中的变量的值。

为了满足我们的需要, x 还需要使用 [@] 后缀进行数组扩展。尤其要注意的是它是 x = $ {high_item} [@] 不是 x = $ {high_item [@] }



你必须在数组上下文中进行评估。否则,它不会像预期的那样工作(如果你做了 arrays = $ {!x} )。

嗯,最后说明: IFS 在这里没有任何区别。只要你在引用的数组上工作, IFS 不会起作用。


I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of the array are seen as 1 item if I leave IFS unchanged here's some sample code:

#!/bin/sh
low1=("AA  QQ" "BB  LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)

for high_item in ${high[@]}
do
   eval arrayz=\${$high_item[@]}
   #IFS=$'\n'
   for item in $arrayz
   do
      echo $item
   done
done

Output:

AA
QQ
BB
LL
CC
DD
EE
FF

As you can see the elements "AA QQ" and "BB LL" have been split.

If I uncomment the line that sets IFS to \n I get the following:

AA QQ BB LL
CC DD
EE FF

Now "AA QQ" and "BB LL" are concatenated!

Is there anyway I can preserve these elements just as they original are...I need the output to look like that:

AA QQ
BB LL
CC 
DD
EE 
FF

解决方案

I think you meant that the output should look like:

AA  QQ
BB  LL
CC
DD
EE
FF

i.e.:

${low1[0]}
${low1[1]}
${low2[0]}
${low2[1]}
${low3[0]}
${low3[1]}

This could be accomplished using:

#!/bin/bash

low1=("AA  QQ" "BB  LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)

for high_item in ${high[@]}
do
    x="${high_item}[@]" # -> "low1[@]"
    arrays=( "${!x}" )

    #IFS=$'\n'
    for item in "${arrays[@]}"
    do
        echo "$item"
    done
done

And please always use #!/bin/bash for bash scripts.

Explanation: ${!x} is indirect variable expansion. It evaluates to the value of variable with a name contained in $x.

For our needs, x needs to have the [@] suffix for array expansion as well. Especially note that it is x=${high_item}[@] and not x=${high_item[@]}.

And you have to evaluate it in array context; otherwise, it wouldn't work as expected (if you do arrays=${!x}).

Ah, and as final note: IFS doesn't make any difference here. As long as you are working on quoted arrays, IFS doesn't come into play.

这篇关于用空格循环数组字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆