zsh-users
 help / color / mirror / code / Atom feed
* join a string with a parameter value if it's not empy
@ 2023-02-03 23:43 sergio
  2023-02-04  0:20 ` Eric Nielsen
  2023-02-04  0:23 ` Eric Nielsen
  0 siblings, 2 replies; 7+ messages in thread
From: sergio @ 2023-02-03 23:43 UTC (permalink / raw)
  To: zsh-users

Hello.

I'd like to prepend a string with a parameter's value + separator.

p='value' -> result: 'value_string'
p=''      -> result: 'string'

I found two solutions:

${p:+${p}_}string
${(j:_:)${=${:-$p string}}}

but both looks too complicated for my feel.
Is there something simpler and prettier?

-- 
sergio.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: join a string with a parameter value if it's not empy
  2023-02-03 23:43 join a string with a parameter value if it's not empy sergio
@ 2023-02-04  0:20 ` Eric Nielsen
  2023-02-04  2:01   ` sergio
  2023-02-04  0:23 ` Eric Nielsen
  1 sibling, 1 reply; 7+ messages in thread
From: Eric Nielsen @ 2023-02-04  0:20 UTC (permalink / raw)
  To: zsh-users, sergio

[-- Attachment #1: Type: text/plain, Size: 169 bytes --]

> p='value' -> result: 'value_string'
> p='' -> result: 'string'

A simpler variation of your first solution:

$p${p:+_}string


--
Sent with HEY <https://hey.com/sent>

[-- Attachment #2: Type: text/html, Size: 2742 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: join a string with a parameter value if it's not empy
  2023-02-03 23:43 join a string with a parameter value if it's not empy sergio
  2023-02-04  0:20 ` Eric Nielsen
@ 2023-02-04  0:23 ` Eric Nielsen
  1 sibling, 0 replies; 7+ messages in thread
From: Eric Nielsen @ 2023-02-04  0:23 UTC (permalink / raw)
  To: zsh-users, sergio

[-- Attachment #1: Type: text/plain, Size: 222 bytes --]

> p='value' -> result: 'value_string'
> p='' -> result: 'string'

A variation of your first solution:

$p${p:+_}string

I don't see how it could be significantly simpler than this.

--
Sent with HEY <https://hey.com/sent>

[-- Attachment #2: Type: text/html, Size: 2805 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: join a string with a parameter value if it's not empy
  2023-02-04  0:20 ` Eric Nielsen
@ 2023-02-04  2:01   ` sergio
  2023-02-04  3:30     ` Eric Nielsen
  2023-02-04  4:03     ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: sergio @ 2023-02-04  2:01 UTC (permalink / raw)
  To: zsh-users

On 04/02/2023 04:20, Eric Nielsen wrote:

> $p${p:+_}string

Nice!

Really I thought about the second way simplification, when I ask this 
question (:

It's more common, as it allows to join several parts.

tmp=($p 'string' whatever else)
${(j.#.)tmp}

Is it possible to construct an array without a tmp parameter? Without 
splitting.

-- 
sergio.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: join a string with a parameter value if it's not empy
  2023-02-04  2:01   ` sergio
@ 2023-02-04  3:30     ` Eric Nielsen
  2023-02-04  4:03     ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Eric Nielsen @ 2023-02-04  3:30 UTC (permalink / raw)
  To: zsh-users, sergio

[-- Attachment #1: Type: text/plain, Size: 164 bytes --]

> Is it possible to construct an array without a tmp parameter?

I can think of:

${(j:_:)${=:-$p string whatever else}}



--
Sent with HEY <https://hey.com/sent>

[-- Attachment #2: Type: text/html, Size: 2728 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: join a string with a parameter value if it's not empy
  2023-02-04  2:01   ` sergio
  2023-02-04  3:30     ` Eric Nielsen
@ 2023-02-04  4:03     ` Bart Schaefer
  2023-02-04  8:39       ` Stephane Chazelas
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2023-02-04  4:03 UTC (permalink / raw)
  To: zsh-users

On Fri, Feb 3, 2023 at 6:01 PM sergio <sergio@outerface.net> wrote:
>
> Is it possible to construct an array without a tmp parameter? Without
> splitting.

If you mean an array value to which you can (in the same operation)
apply expansion flags such as joining, not really, no.

If you mean without having to declare a temporary name, you can use
the positional parameters:

set -- $p 'string' whatever else
print -r -- "${(j.#.)@}"

Of course that means knowing $@, $1, $2, etc. aren't being used for
anything else.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: join a string with a parameter value if it's not empy
  2023-02-04  4:03     ` Bart Schaefer
@ 2023-02-04  8:39       ` Stephane Chazelas
  0 siblings, 0 replies; 7+ messages in thread
From: Stephane Chazelas @ 2023-02-04  8:39 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

2023-02-03 20:03:16 -0800, Bart Schaefer:
[...]
> If you mean without having to declare a temporary name, you can use
> the positional parameters:
> 
> set -- $p 'string' whatever else
> print -r -- "${(j.#.)@}"
> 
> Of course that means knowing $@, $1, $2, etc. aren't being used for
> anything else.

Or use an anonymous function:

(){ print -r -- ${(j[#])@}; } 'some string' $'whatever\nelse'

-- 
Stephane


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-02-04  8:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-03 23:43 join a string with a parameter value if it's not empy sergio
2023-02-04  0:20 ` Eric Nielsen
2023-02-04  2:01   ` sergio
2023-02-04  3:30     ` Eric Nielsen
2023-02-04  4:03     ` Bart Schaefer
2023-02-04  8:39       ` Stephane Chazelas
2023-02-04  0:23 ` Eric Nielsen

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).