Feature Request: Python-like string split and join

2021-01-23 Thread William Park
If separator is a single character, then shell can split a string or
join arguments.  But, if separator is string, then it gets messy.  We
shouldn't have to resort to Awk/Python for that.

How about Python-like string split and string join?

${parameter...} is overloaded as is, and we are running out of special
characters.  So, how about, just appending at the end of parameter
expansion syntax?  
${parameter...;sep}
where 
${parameter...} is already existing expansion, and
sep is substring to join, if ${parameter...} is multiple argument
syntax involving '*', like ${*}, ${*:10:10}, ${array[*]:20}, and
sep is substring to split on, if ${parameter...} is single string
syntax like ${var}, ${var:3:10}, ${1}, ${10:2:4}

Eg.
array=( "${var;.}" )
would be equivalent to
IFS=. read -a array <<< "$var"

Eg.
echo "${*;.}"
would be equivalent to
(IFS=.; echo "$*")

Where it would be used?
- everywhere

Advantage?
- replaces multiple lines in shell function
- makes Bash script simpler and clearer
- less bookkeeping and less mistakes

-- 
William Park 



Re: Feature Request: Python-like string split and join

2021-01-23 Thread Koichi Murase
2021年1月24日(日) 10:22 William Park :
> [...]  But, if separator is string, then it gets messy.

I agree that the split by a (multi-character) string is one of the
non-trivial operations in the current Bash.

> We shouldn't have to resort to Awk/Python for that.

I think a typical reply in this list to this kind of request is just
"use Awk/Python". Bash doesn't aim to be a language for writing real
programs, so usually you should write the entire program in another
language. That said, I feel splitting/joining with a string separator
is useful and primitive enough.

> How about Python-like string split and string join?

What does "Python-like" mean? Is it special compare to common
split/join in typical programming languages?

> ${parameter...;sep}

Is there a background for choosing a semicolon to split words? Zsh has
a similar feature with a different syntax:

${(s:sep:)parameter...}
${(j:sep:)parameter...}

Actually, we can think of dozens of fancy string operations found in
other languages, and the split or the join is merely one of such
operations. For example, Zsh has about 50 different flags modifying
the expansion results ( see
http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
). We cannot prepare different syntaxes for each of these possible 50
operations, so the question is whether the split/join is so important
and essential compared to other possible string operations that it
deserves to occupy the semicolon.

Recent Bash rather has the parameter expansion form of ${param@flags},
so, if it would be implemented, I guess something like

${parameter@s:sep:}
${parameter@j:sep:}

would be a more moderate choice.

--
Koichi



Re: Feature Request: Python-like string split and join

2021-01-23 Thread William Park
On Sun, Jan 24, 2021 at 11:29:46AM +0800, Koichi Murase wrote:
> 2021年1月24日(日) 10:22 William Park :
> Is there a background for choosing a semicolon to split words? Zsh has
> a similar feature with a different syntax:

I'm running out of special characters, and I don't want another operator
or syntax that I can't remember 2 weeks from now. :-) That's my main
issue with Zsh.

-- 
William Park