如何格式化和遍历包含数组的数组,并且每个数组包含一个数组? [英] How do format and traverse an array that contains arrays ,and each array contains an array?

查看:25
本文介绍了如何格式化和遍历包含数组的数组,并且每个数组包含一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在数组中包含的数组中创建多维.

I'm trying to create multidimensional within arrays that are contained in an array.

tests=("'0' '1 2'" "'4' '5 6'")

在测试中的每个数组中,我都希望有子数组.使用第一个数组'0''1,2'",使另一个for循环遍历子数组的内容.

in each array within tests, I want to have sub arrays. With the first array "'0' '1,2'", make another for loop to go through the contents of the sub array.

推荐答案

自bash 4.3起.(3个级别,第一个仅包含一个用于演示的元素):

Since bash 4.3. (3 levels, the first contains only one element for the demo):

arr01=(0 '1 2')
arr02=(4 '5 6')
arr1=(arr01 arr02)
arr=(arr1)

declare -n elmv1 elmv2

for elmv1 in "${arr[@]}"; do
    for elmv2 in "${elmv1[@]}"; do
        for elm in "${elmv2[@]}"; do
            echo "<$elm>"
        done
    done
done

4.3之前

arr01=(0 '1 2')
arr02=(4 '5 6')
arr1=('arr01[@]' 'arr02[@]')
arr=('arr1[@]')

for elmv1 in "${arr[@]}"; do
    for elmv2 in "${!elmv1}"; do
        for elm in "${!elmv2}"; do
            echo "<$elm>"
        done
    done
done

这篇关于如何格式化和遍历包含数组的数组,并且每个数组包含一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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