简体   繁体   English

修改数组中逗号分隔值的最简单方法?

[英]Simplest way to modify comma-separated values in an array?

Let's say I have an array of n elements.假设我有一个包含 n 个元素的数组。 Each element is a string of comma-separated x,y coordinate pairs, eg "581,284".每个元素都是一串以逗号分隔的 x,y 坐标对,例如“581,284”。 There is no set character length to these x,y values.这些 x,y 值没有设置字符长度。

Say I wanted to subtract 8 from each x value, and 5 from each y value.假设我想从每个 x 值中减去 8,从每个 y 值中减去 5。

What would be the simplest way to modify x and y, independently of each other, without permanently splitting the x and y values apart?修改 x 和 y 的最简单方法是什么,它们彼此独立,而不会将 x 和 y 值永久分开?

eg the first array element "581,284" becomes "573,279", the second array element "1013,562" becomes "1005,274", and so forth.例如,第一个数组元素“581,284”变为“573,279”,第二个数组元素“1013,562”变为“1005,274”,以此类推。

I worked on this problem for a couple of hours (I'm an amateur at bash), and it seemed as if my approach was awfully convoluted.我在这个问题上工作了几个小时(我是 bash 的业余爱好者),看起来我的方法非常复杂。

Please note that the apostrophes above are only added for emphasis, and are not a part of the problem.请注意,上面的撇号只是为了强调,而不是问题的一部分。

Thank you in advance, I've been racking my head over this for a while now!提前谢谢你,我已经为此苦苦挣扎了一段时间了!

Edit: The following excerpt is the approach I was taking.编辑:以下摘录是我采用的方法。 I don't know much about bash, as you can tell.如您所知,我对 bash 了解不多。

   while read value
   do
     if [[ -z $offset_list ]]
     then
       offset_list="$value"
     else
       offset_list="$offset_list,$value"
     fi
   done < text.txt
   new_offset=${offset_list//,/ }
   read -a new_array <<< $new_offset

   for value in "${new_array[@]}"
     do
       if [[ $((value%2)) -eq 1 ]]
       then
         value=$((value-8));
         new_array[$counter]=$value
         counter=$((counter+1));
       elif [[ $((value%2)) -eq 0 ]]
       then
         value=$((value-5));
         new_array[$counter]=$value
         counter=$((counter+1));
       fi
     done

Essentially I had originally read the coordinate pairs, and stripped the commas from them, and then planned on modifying odd/even values which were populated into the new array.本质上,我最初阅读了坐标对,并从它们中去掉了逗号,然后计划修改填充到新数组中的奇数/偶数值。 At this point I realized that there had to be a more efficient way.在这一点上,我意识到必须有一种更有效的方法。

I believe the following should achieve what you are looking for:我相信以下内容应该可以实现您的目标:

#!/bin/bash

input=("581,284" "1013,562")

echo "Initial array ${input[@]}"
for index in ${!input[@]}; do
    value=${input[$index]}
    x=${value%%,*}
    y=${value##*,}

    input[$index]="$((x-8)),$((y+5))"
done

echo "Modified array ${input[@]}"

${!input[@]} allows us to loop over the indexes of the bash array. ${!input[@]}允许我们遍历 bash 数组的索引。

${value%%,*} and ${value##*,} relies on bash parameter substitution to remove the everything after or before the comma (respectively). ${value%%,*}${value##*,}依赖于 bash 参数替换来删除逗号之后或之前的所有内容(分别)。 This effectively splits your string into two variables.这有效地将您的字符串拆分为两个变量。

From there, it's your required math and variable reassignment to mutate the array.从那里开始,您需要重新分配数学和变量来改变数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM