Bash-从文件填充2D数组 [英] Bash - populate 2D array from file
问题描述
我知道这可能很容易,但是我为此非常努力.
问题描述:
我有一个坐标为以下格式的文本文件:
1 2
3 7
...
其中第一列=='x'和第二列=='y'坐标.
现在,我想通过使用文件中的此坐标填充"N"和"M"的2D数组,方法是为文件和."中指定的点打印"X".否则.
I know it's probably something easy but i'm struggling really hard with this.
Problem description:
I have a text file with coordinates in format:
1 2
3 7
...
where first column == 'x' and second column == 'y' coordinate.
Now i want to populate a 2D array of size N x M using this coordinates from file by printing 'X' for points that are specified in file and '.' otherwise.
示例:array [1] [2] ='X',array [3] [7] ='X',array [0] [1] ='.'
Example: array[1][2] = 'X', array[3][7] = 'X', array[0][1] = '.'
到目前为止,我已经尝试过:
So far I have tried:
-
分隔x,y列并将其存储在这样的数组中:
separating x,y columns and storing them in arrays like this:
xcoords="xcoords.txt"
ycoords="ycoords.txt"
head $geneFile | grep " " | awk -F' ' '{print $1}' > $xcoords
head $geneFile | grep " " | awk -F' ' '{print $2}' > $ycoords
readarray -t xarr < $xcoords
readarray -t yarr < $ycoords
但是不能真正从这里移走(最后以无效的3,4为循环嵌套).
But can't really move from here (ended up with not-working 3,4 nested for loops).
-
或者仅将文件存储在2D数组中.但是由于bash不支持2D数组(我知道有一些方法可以对其进行仿真,但是在这种情况下不知道如何使用它)
Or just storing a file in a 2D array. But since bash does not support 2D arrays (I know there are some ways to simulate it but don't know how to use it in this case)
readarray -t array < $geneFile #
这样的循环会很棒-当然,我想要的不是固定值,而是"$ {xarr [i]}".
Loops like this would be great - of course instead of fixed values I'd like to get something like "${xarr[i]}".
for (( i = 0; i < nRows; i++ )); do
for (( j = 0; j < nColumns; j++ )); do
if [[ $i == 5 ]] && [[ $j == 5 ]]; then # of course instead of fixed value here I'd like to get coordinates values.
printf "O"
else
printf "."
fi
done
printf "\n"
done
任何建议/示例如何实现?预先感谢!
Any advice/example how to achieve it? Thanks in advance!
推荐答案
有一种简单的方法可以将大小为M
×N
的二维数组编码为大小为M*N
的一维数组,用于例如:
There's an easy way to encode a 2-dimensional array of size M
×N
into a 1-dimensional array of size M*N
, for example:
A[p,q] ↔ B[p+q*M]
对于我们的任务:我们将从头修复M
和N
,将所有数组项设置为.
,然后读取文件以将相应的字段设置为X
:
For our task: we'll fix M
and N
from the beginning, set all the array terms to .
and then read the file to set the corresponding fields to X
:
#!/bin/bash
M=10
N=10
[[ $1 && -f $1 && -r $1 ]] || { printf >&2 'arg must be readable file.\n'; exit; }
geneFile=$1
array=()
for ((i=0; i<M*N; ++i)); do
array+=( '.' )
done
while read -r x y; do
[[ $x && $y ]] || continue
[[ $x = +([[:digit:]]) && $y = +([[:digit:]]) ]] || continue
((x=10#$x,y=10#$y))
(( x<M && y<N )) || continue
array[x+y*M]=X
done < "$geneFile"
# print to stdout
for((i=0;i<N;++i)); do
printf '%s' "${array[@]:i*M:M}" $'\n'
done
根据您的数据,输出为:
With your data, output is:
..........
..........
.X........
..........
..........
..........
..........
...X......
..........
..........
在这种情况下,另一种可能性是使用字符串而不是数组(详细信息留给阅读器或其他答复者).
In this case, another possibility is to use a string instead of an array (details left to the reader or to another answerer).
注意:读取文件的循环会静默丢弃格式错误的行.
Note: the loop that reads the file silently discards malformed lines.
这篇关于Bash-从文件填充2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!