zsh-users
 help / color / mirror / code / Atom feed
* Removing an element from an array
@ 2020-11-02 11:11 Peter Slížik
  2020-11-02 11:25 ` Roman Perepelitsa
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Slížik @ 2020-11-02 11:11 UTC (permalink / raw)
  To: zsh-users

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

Hello,

I would like to remove an element from an array - actually a path from the
$path variable. After some googling, I've discovered the ${array:|excl}
syntax.

Here is my code:

excl=($path_to_remove)
echo "path type: ${(t)path}
# -> path type: array-tied-export-special

export path=${path:|excl}
# -> export:19: path: inconsistent type for assignment

What am I doing wrong? Actually, as the command ends up with an exception,
I'm not able to verify whether 'path' is an array or a scalar after the
command.

This is the relevant part of the documentation. I don't think any of the
conditions in the second sentence is relevant to the error message.

${name:|arrayname}

If arrayname is the name (N.B., not contents) of an array variable, then
any elements contained in arrayname are removed from the substitution of
name. If the substitution is scalar, either because name is a scalar
variable or the expression is quoted, the elements of arrayname are instead
tested against the entire expression
Any suggestions?
Peter

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

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

* Re: Removing an element from an array
  2020-11-02 11:11 Removing an element from an array Peter Slížik
@ 2020-11-02 11:25 ` Roman Perepelitsa
  2020-11-02 12:23   ` Peter Slížik
  0 siblings, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2020-11-02 11:25 UTC (permalink / raw)
  To: Peter Slížik; +Cc: Zsh Users

On Mon, Nov 2, 2020 at 12:11 PM Peter Slížik <peter.slizik@gmail.com> wrote:
>
> I would like to remove an element from an array - actually a path from the $path variable. After some googling, I've discovered the ${array:|excl} syntax.
>
> Here is my code:
>
> excl=($path_to_remove)
> export path=${path:|excl}

This is pretty close. Here's the correct syntax:

    excl=($path_to_remove)
    path=(${path:|excl})

However, if you want to remove just one element, there is a simpler way:

    path=(${path:#$path_to_remove})

Roman.


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

* Re: Removing an element from an array
  2020-11-02 11:25 ` Roman Perepelitsa
@ 2020-11-02 12:23   ` Peter Slížik
  2020-11-02 18:31     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Slížik @ 2020-11-02 12:23 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

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

Dear Roman,

thank you for both suggestions.

By the way,  can I ask why it is necessary to re-introduce the array
context by another pair of parentheses, if the expression already produces
an array value?

Or does the ${...:|...} expression produce a string which needs to be
parsed again? Would this cause problems if any of the path elements
contained a space in the file name?

Thank you,
Peter

пон, 2. нов 2020. у 12:25 Roman Perepelitsa <roman.perepelitsa@gmail.com>
је написао/ла:

> On Mon, Nov 2, 2020 at 12:11 PM Peter Slížik <peter.slizik@gmail.com>
> wrote:
> >
> > I would like to remove an element from an array - actually a path from
> the $path variable. After some googling, I've discovered the ${array:|excl}
> syntax.
> >
> > Here is my code:
> >
> > excl=($path_to_remove)
> > export path=${path:|excl}
>
> This is pretty close. Here's the correct syntax:
>
>     excl=($path_to_remove)
>     path=(${path:|excl})
>
> However, if you want to remove just one element, there is a simpler way:
>
>     path=(${path:#$path_to_remove})
>
> Roman.
>

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

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

* Re: Removing an element from an array
  2020-11-02 12:23   ` Peter Slížik
@ 2020-11-02 18:31     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2020-11-02 18:31 UTC (permalink / raw)
  To: Peter Slížik; +Cc: Zsh Users

On Mon, Nov 2, 2020 at 4:23 AM Peter Slížik <peter.slizik@gmail.com> wrote:
>
> By the way,  can I ask why it is necessary to re-introduce the array context by another pair of parentheses, if the expression already produces an array value?

Assignment without the parens is a scalar context; the existing type
of the variable named on the left side is not considered during
parsing, and if the variable is not "special" the type may actually be
changed from array to scalar by the assignment operation.


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

end of thread, other threads:[~2020-11-02 18:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-02 11:11 Removing an element from an array Peter Slížik
2020-11-02 11:25 ` Roman Perepelitsa
2020-11-02 12:23   ` Peter Slížik
2020-11-02 18:31     ` Bart Schaefer

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).