zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: jamil bio <jamilbio20@gmail.com>, zsh-workers@zsh.org
Subject: Re: zsh adds empty string to local parameter when += is used
Date: Wed, 20 Jan 2021 16:26:03 +0000 (GMT)	[thread overview]
Message-ID: <1204351835.3138771.1611159963588@mail2.virginmedia.com> (raw)
In-Reply-To: <a7422e8c-48cd-23eb-8fa9-66a85f0a9ad5@gmail.com>

> On 20 January 2021 at 16:16 jamil bio <jamilbio20@gmail.com> wrote:
> func()
> {
>      local REPLY  files
> 
>      #unset files
>      for REPLY in file1 file2 file3
>      do
>          files+=( "$REPLY" )
>          echo "${#files[@]} -- ${files[@]}"
>      done
> }
> 
> func
> 
> 2 --  file1
> 3 --  file1 file2
> 4 --  file1 file2 file3

Yes, this is a bit weird, but it's explicable.

"local files" makes the variable "files" into a scalar, so if it's ever
referred to it will return an empty string.

The first time the function executes

  files+-( "$REPLY" )

it retrieves the existing $files.  At this point that's just an empty
string.  Then it adds the new element and assigns back to files.  It's
only at this point that files becomes an array.

You can fix the problem by declaring files as an array,

local -a files

pws


  parent reply	other threads:[~2021-01-20 16:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-20 16:16 jamil bio
2021-01-20 16:22 ` Roman Perepelitsa
2021-01-20 16:26 ` Peter Stephenson [this message]
2021-01-20 16:29   ` Roman Perepelitsa
2021-01-20 16:31 ` Daniel Shahaf
2021-01-20 16:40   ` jamil bio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1204351835.3138771.1611159963588@mail2.virginmedia.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=jamilbio20@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).