References
Last active
July 28, 2022 10:56
-
-
Save kvpb/2d2873a19ef18d78d9876af08f8c8c5c to your computer and use it in GitHub Desktop.
Test Script 70: BASH pseudo-multidimensional array iteration over any dimension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
var1=([0]="first" [1]="second" [2]="third" [5]="sixth" [10]="eleventh"); | |
n=([0]="0" [1]="1" [2]="2" [5]="5" [10]="10"); | |
var2=([0]="zero" [1]="one" [2]="two" [5]="five" [10]="ten"); | |
printf '\n`echo \"${var1[@]}\"` s output:\t'; echo "${var1[@]}"; | |
printf '`echo \"${!var1[@]}\"` s output:\t'; echo "${!var1[@]}"; | |
printf '\nWith `${var1[@]}`:\n'; | |
for i in "${var1[@]}"; | |
do | |
echo "${var1[i]}"" "$'\t'"${n[i]}"$'\t'"${var2[i]}"; | |
done; | |
printf '... BASH gets the wrong value, ${var1\e[9m[i]\e[29m}, thus it shits in the glue.\n'; | |
printf '\nWith `${!var1[@]}`:\n'; | |
for i in "${!var1[@]}"; | |
do | |
echo "${var1[i]}"" "$'\t'"${n[i]}"$'\t'"${var2[i]}"; | |
done; | |
printf '... BASH gets the right value, ${\e[8m!\e[28m\e[9mvar1[\e[29mi\e[9m]\e[29m}, thus it does well.\n'; | |
printf '\n'; | |
# script70.sh | |
# Test Script 70: BASH pseudo-multidimensional array iteration over any dimension | |
# | |
# Author: Karl V. P. Bertin `kvpb` | |
# Phone number: +33 A BB BB BB BB | |
# Email: local-part@domain | |
# LinkedIn: https://www.linkedin.com/in/karlbertin | |
# Facebook: https://www.facebook.com/profile.php?id= | |
# Instagram: https://www.instagram.com/karlbertin/ | |
# Snapchat: https://www.snapchat.com/add/karlbertin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var=([0]="value 1" [2]="value 2" [10]="value 3" [11]="value 4"); x=([0]="1" [1]="jeej" [2]="le,kk" [10]="pet de vache"); for i in ${var[@]}; do echo "${var[i]}"$'\t'$'\t'"${x[i]}"; done | |
var1=([0]="0" [1]="1" [2]="2" [5]="5" [10]="10"); var2=([0]="zero" [1]="one" [2]="two" [5]="five" [10]="ten"); for i in ${var1[@]}; do echo "${var1[i]}"" "$'\t'"${var2[i]}"; done # fucky: var1=([0]="a" [1]="b" [2]="c" [5]="f" [10]="j"); var2=([0]="zero" [1]="one" [2]="two" [5]="five" [10]="ten"); for i in ${var1[@]}; do echo "${var1[i]}"" "$'\t'"${var2[i]}"; done | |
var1=([0]="0" [1]="1" [2]="2" [5]="5" [10]="10"); var2=([0]="zero" [1]="one" [2]="two" [5]="five" [10]="ten"); for i in ${var1[@]}; do echo "${var1[i]}"" "$'\t'"${var2[i]}"; done | |
var1=([0]="a" [1]="b" [2]="c" [5]="f" [10]="j"); var2=([0]="zero" [1]="one" [2]="two" [5]="five" [10]="ten"); for i in ${var1[@]}; do echo "${var1[i]}"" "$'\t'"${var2[i]}"; done | |
var1=([0]="a" [1]="b" [2]="c" [5]="f" [10]="j"); var2=([0]="zero" [1]="one" [2]="two" [5]="five" [10]="ten"); for ((i=0; i<${#var1[@]}; i++)); do echo "${var1[i]}"" "$'\t'"${var2[i]}"; done | |
echo ${var1[*]}; echo ${var1[@]}; echo "${var1[@]}"; echo "${!var1[@]}" | |
var1=([0]="a" [1]="b" [2]="c" [5]="f" [10]="j"); var2=([0]="zero" [1]="one" [2]="two" [5]="five" [10]="ten"); for i in "${!var1[@]}"; do echo "${var1[i]}"" "$'\t'"${var2[i]}"; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment