Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the index for looping through the array #1129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

4ooc
Copy link

@4ooc 4ooc commented Sep 23, 2022

Closes #1125

@4ooc 4ooc closed this Oct 27, 2022
@4ooc 4ooc removed their assignment Oct 27, 2022
@4ooc 4ooc reopened this May 10, 2023
Use `${#array[@]}` to return the correct length;
do't subtract 1 as the default last index of an array in Zsh is its length.

(cherry picked from commit 63f67ab)
@4ooc 4ooc changed the title Change the way to loop through array(Closes #1125) Fix the index for looping through the array May 10, 2023
@4ooc
Copy link
Author

4ooc commented May 10, 2023

Hi, @helpermethod @marc0der
I’m curious as to why you weren’t able to replicate issue #1125, as I’m also using the newest version of zsh (build from the latest commit) and I can still reproduce it.

Is there anything different about ZSH for macOS compared to other systems? Or do you use setopt ksh_arrays?

And this code is incorrect in both Bash and Zsh, even though it produces the desired result:

for ((i = ${#versions} - 1; i >= 0; i--)); do

In bash, #{versions} return the length of #versions[0](the command result is always right because #versions[0] is much longer than array's length).

bash-5.2$ bash --version
GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
....
bash-5.2$ versions=("1" "2" "3"); echo ${#versions}
1
bash-5.2$ versions=("$PWD" "2" "3"); echo ${#versions}
69
bash-5.2$ versions=("$PWD" "2" "3"); for ((i = ${#versions} - 1; i >= 0; i--)); do echo "index-$i ${versions[$i]}"; done
index-68
...
...
index-2 3
index-1 2
index-0 /private/var/folders/hz/zlxrt0dn56gcyygny7mmyb_h0000gn/T/tmp.ShxROGHO

In zsh, the last index of versions is missing.

Look%  zsh --version
zsh 5.9.0.1-dev (arm-apple-darwin22.4.0)
Look%  versions=("1234567" "2" "3"); for ((i = ${#versions} - 1; i >= 0; i--)); do echo "index-$i ${versions[$i]}"; done
index-2 2
index-1 1234567
index-0
Look% setopt KSH_arrays
Look% setopt | grep -i KSHarrays
ksharrays
Look% versions=("$PWD" "2" "3"); for ((i = ${#versions} - 1; i >= 0; i--)); do echo "index-$i ${versions[$i]}"; done
index-68
...
...
index-2 3
index-1 2
index-0 /private/var/folders/hz/zlxrt0dn56gcyygny7mmyb_h0000gn/T/tmp.ShxROGHO

Use ${#array[@]} to return the correct length, and do't subtract 1 as the default last index of an array in Zsh is its length.
Now

Look% versions=("$PWD" "2" "3"); for ((i = ${#versions[@]}; i >= 0; i--)); do echo "index-$i ${versions[$i]}"; done
index-3 3
index-2 2
index-1 /private/var/folders/hz/zlxrt0dn56gcyygny7mmyb_h0000gn/T/tmp.107a1NQ3
index-0
Look% bash
bash-5.2$ versions=("$PWD" "2" "3"); for ((i = ${#versions[@]}; i >= 0; i--)); do echo "index-$i ${versions[$i]}"; done
index-3
index-2 3
index-1 2
index-0 /private/var/folders/hz/zlxrt0dn56gcyygny7mmyb_h0000gn/T/tmp.107a1NQ3
bash-3.2$ versions=("$PWD" "2" "3"); for ((i = ${#versions[@]}; i >= 0; i--)); do echo "index-$i ${versions[$i]}"; done
index-3
index-2 3
index-1 2
index-0 /private/var/folders/hz/zlxrt0dn56gcyygny7mmyb_h0000gn/T/tmp.107a1NQ3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: sdk list java in offline mode, the installed list is incomplete
1 participant