索引数组:1到数组的长度

时间:2021-11-27 21:44:44

Sorry for a bad title, please feel free to adjust it to something more appropriate.

抱歉标题不好,请随意将其调整为更合适的标题。

How can I index arrays using zsh or bash scripting as I am doing for lists in R below;

如何使用zsh或bash脚本编制索引数组,就像我在下面的R中为列表所做的那样;

# Some lists with the same number of elements
list1 <- list(sample(letters,10))
list2 <- list(1:10)

for(i in 1:length(list1)) {
  a <- list1[[1]][i]
  b <- list2[[1]][i]
}

print(paste(a,b))  # Or some other function where a and b are used simultaneously

[1] "f 1"
[1] "e 2"
[1] "t 3"
[1] "s 4"
[1] "c 5"
[1] "o 6"
[1] "p 7"
[1] "y 8"
[1] "k 9"
[1] "d 10"

The below code obviously only prints the last elements from both lists, since I have not found a way to do 1 to the length of array

下面的代码显然只打印两个列表中的最后一个元素,因为我还没有找到一种方法来做1到数组的长度

# dummy data

echo 'A 1' > A.txt
echo 'B 1' > B.txt
echo 'C 1' > C.txt

echo 'A,2' > A.csv
echo 'B,2' > B.csv
echo 'C,2' > C.csv

txtlist=(*.txt) # create an array of .txt files
csvlist=(*.csv) # create an array of .csv files

# in zsh $#array returns the length of the array, so 

for i in $#txtlist; do
    a=$txtlist[i]
    b=$csvlist[i]

    echo $a,$b  # Or some other function where a and b are used simultaneously

done

#C.txt,C.csv

Any pointers would be very much appreciated, thanks!

任何指针都非常感谢,谢谢!

3 个解决方案

#1


4  

bash and zsh both know C-style for-loops:

bash和zsh都知道C风格的for循环:

From man 1 zshmisc (man 1 bash is essentially the same):

从man 1 zshmisc(man 1 bash基本上是相同的):

for (( [expr1] ; [expr2] ; [expr3] )) do list done
       The  arithmetic expression expr1 is evaluated first (see the section `Arithmetic Evaluation').  The arithmetic expression expr2 is repeatedly
       evaluated until it evaluates to zero and when non-zero, list is executed and the arithmetic expression expr3 evaluated.  If any expression is
       omitted, then it behaves as if it evaluated to 1.

Example for zsh:

zsh示例:

for (( i=1; i<=$#txtlist; i++ )); do
    echo "$txtlist[$i]" "$csvlist[$i]"
done

Example for bash:

bash的例子:

for (( i=0; i<=${#txtlist[@]}; i++ )); do
    echo "${txtlist[$i]}" "${csvlist[$i]}"
done

#2


2  

I am not sure to understand your example, I'm sorry, but, you can do loop like that in bash :

我不太明白你的例子,对不起,但是,你可以在bash中做这样的循环:

myLength=${#myArray[@]}
for (( i=1; i<${myLength}; i++ ));
do
  echo ${myArray[$i]}
done

#3


1  

Use the following syntax:

使用以下语法:

$ x=(1 2 3 4 5)
$ print $x
1 2 3 4 5
$ print ${x:1}
2 3 4 5

#1


4  

bash and zsh both know C-style for-loops:

bash和zsh都知道C风格的for循环:

From man 1 zshmisc (man 1 bash is essentially the same):

从man 1 zshmisc(man 1 bash基本上是相同的):

for (( [expr1] ; [expr2] ; [expr3] )) do list done
       The  arithmetic expression expr1 is evaluated first (see the section `Arithmetic Evaluation').  The arithmetic expression expr2 is repeatedly
       evaluated until it evaluates to zero and when non-zero, list is executed and the arithmetic expression expr3 evaluated.  If any expression is
       omitted, then it behaves as if it evaluated to 1.

Example for zsh:

zsh示例:

for (( i=1; i<=$#txtlist; i++ )); do
    echo "$txtlist[$i]" "$csvlist[$i]"
done

Example for bash:

bash的例子:

for (( i=0; i<=${#txtlist[@]}; i++ )); do
    echo "${txtlist[$i]}" "${csvlist[$i]}"
done

#2


2  

I am not sure to understand your example, I'm sorry, but, you can do loop like that in bash :

我不太明白你的例子,对不起,但是,你可以在bash中做这样的循环:

myLength=${#myArray[@]}
for (( i=1; i<${myLength}; i++ ));
do
  echo ${myArray[$i]}
done

#3


1  

Use the following syntax:

使用以下语法:

$ x=(1 2 3 4 5)
$ print $x
1 2 3 4 5
$ print ${x:1}
2 3 4 5