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

Arrays concept part 2 #729

Merged
merged 5 commits into from
Feb 23, 2025
Merged

Arrays concept part 2 #729

merged 5 commits into from
Feb 23, 2025

Conversation

glennj
Copy link
Contributor

@glennj glennj commented Feb 21, 2025

No description provided.

@glennj glennj requested a review from a team February 21, 2025 04:09
echo "${myarray[*]}"
```

You are required to enclose the expansion in double quotes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no actual "requirement" as much as a "should" to avoid glob expansion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirement is that the concatenate behaviour only applies when the expansion is quoted. Otherwise you get word splitting.

In the manual:

If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS variable,

# => "one:two:three:four"
```

Localizing `IFS` in the function means we don't have to save the old value and restore it back to it's previous value in the global scope.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW a subshell is a (simpler?) alternative.

array=(one two three)
( IFS=,; echo "${array[*]}"; )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add it

set -- one two three
set -- "$@" four

for item in "$@"; do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW you can also omit the in "$@"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but let's be explicit

glennj and others added 2 commits February 22, 2025 09:49
Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com>
@glennj glennj marked this pull request as ready for review February 23, 2025 02:20
@glennj glennj merged commit f2ad61c into exercism:main Feb 23, 2025
2 checks passed
@glennj glennj deleted the arrays-2 branch February 23, 2025 02:29
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.

2 participants