Bash 二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22581475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash two dimensional arrays
提问by Vrsi
:) well i want to read line and save first and second column from file.o And i want to save it into two dimensional array in bash. Filtration is ok its doing the right think. BUT i dont know how to check if the array is empty ( i mean that it saved nothing to that array so its all from the file) . Secondly im getting error on FUNCTION and RELIANCE that command was not found and again echo is not working, tried to google everything but it seems that no one is working with two indexed dimensional arrays. Ty for any tip!
:) 好吧,我想读取 line 并从 file.o 中保存第一列和第二列,并且我想将它保存到 bash 中的二维数组中。过滤是好的,它做正确的思考。但是我不知道如何检查数组是否为空(我的意思是它没有将任何内容保存到该数组中,因此全部来自文件)。其次,我在 FUNCTION 和 RELIANCE 上遇到错误,该命令未找到,并且 echo 再次不起作用,试图用谷歌搜索所有内容,但似乎没有人在使用两个索引维数组。任何提示!
#!/bin/bash
declare -a NAMES
declare -a FUNCTION
declare -a RELIANCE
index=1
index1=1
for a in file.o
do
NAMES[$index]=$a
until [ nm file.o | grep -o '[UTBGCD].*' | awk '{print }' | awk "NR==$index1" -eq 0 ]
do
FUNCTION[$index][$index1]=$( nm file.o | grep -o '[UTBGCD].*' | awk '{print }' | awk "NR==$index1" )
RELIANCE[$index][$index1]=$( nm filea.o | grep -o '[UTBGCD].*' | awk '{print }' | awk "NR==$index1" )
echo ${FUNCTION[$index][$index1]}
index1=$((index1+1))
done
index=$((index+1))
done
回答by Charles Duffy
Bash does not support multidimensional arrays. However, you can fake it in one of two ways. First, there's associative arrays:
Bash 不支持多维数组。但是,您可以通过以下两种方式之一伪造它。首先,有关联数组:
declare -A array
x=1 y=2
array["${x}_$[y}"]=value
Second, with fixed-size arrays, you can simply do some math.
其次,对于固定大小的数组,您可以简单地做一些数学运算。
declare -a array
x_max=100
x=1 y=2
array[(y*x_max)+x]=value