zsh-workers
 help / color / mirror / code / Atom feed
* zsh adds empty string to local parameter when += is used
@ 2021-01-20 16:16 jamil bio
  2021-01-20 16:22 ` Roman Perepelitsa
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: jamil bio @ 2021-01-20 16:16 UTC (permalink / raw)
  To: zsh-workers

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

Hello good people,

It seems there is a bug which I will describe below.
I checked with usenet comp.unix.shell people and they said that is 
strange behavior.

I am using Z-shell 5.8. The problem is when I add elements to an array 
with += inside a function, an empty element is generated. The empty 
element is added only if I set array $files as local.

The following is an example code.


#!/bin/zsh
#!/bin/bash

func()
{
     local REPLY  files

     #unset files
     for REPLY in file1 file2 file3
     do
         files+=( "$REPLY" )
         echo "${#files[@]} -- ${files[@]}"
     done
}

func


If I set $files to local but unset it before the loop, the empty element 
is not added. Running the script with zsh returns:


2 --  file1
3 --  file1 file2
4 --  file1 file2 file3


While if we run the code with bash:


1 -- file1
2 -- file1 file2
3 -- file1 file2 file3


I was actually using a while read loop to read filenames, but the 
problem also occurs when I use the for loop.

So the workaround suggested is to set $files as local and then later 
define files=( ) which will prevent empty element being added somehow.

Thanks for all your efforts in making zsh so great!

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

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

* Re: zsh adds empty string to local parameter when += is used
  2021-01-20 16:16 zsh adds empty string to local parameter when += is used jamil bio
@ 2021-01-20 16:22 ` Roman Perepelitsa
  2021-01-20 16:26 ` Peter Stephenson
  2021-01-20 16:31 ` Daniel Shahaf
  2 siblings, 0 replies; 6+ messages in thread
From: Roman Perepelitsa @ 2021-01-20 16:22 UTC (permalink / raw)
  To: jamil bio; +Cc: Zsh hackers list

On Wed, Jan 20, 2021 at 5:17 PM jamil bio <jamilbio20@gmail.com> wrote:
>
> Hello good people,
>
> It seems there is a bug which I will describe below.
> I checked with usenet comp.unix.shell people and they said that is strange behavior.

I agree that it's strange. It's surprising to me (although I'm far
from being an expert in zsh).

Simpler test case:

    % zsh -fc 'typeset x; x+=(); typeset -p x'
    typeset -a x=( '' )


Roman.


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

* Re: zsh adds empty string to local parameter when += is used
  2021-01-20 16:16 zsh adds empty string to local parameter when += is used jamil bio
  2021-01-20 16:22 ` Roman Perepelitsa
@ 2021-01-20 16:26 ` Peter Stephenson
  2021-01-20 16:29   ` Roman Perepelitsa
  2021-01-20 16:31 ` Daniel Shahaf
  2 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2021-01-20 16:26 UTC (permalink / raw)
  To: jamil bio, zsh-workers

> 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


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

* Re: zsh adds empty string to local parameter when += is used
  2021-01-20 16:26 ` Peter Stephenson
@ 2021-01-20 16:29   ` Roman Perepelitsa
  0 siblings, 0 replies; 6+ messages in thread
From: Roman Perepelitsa @ 2021-01-20 16:29 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: jamil bio, Zsh hackers list

On Wed, Jan 20, 2021 at 5:26 PM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> Yes, this is a bit weird, but it's explicable.

Does the following look inconsistent though?

    % zsh -fc 'typeset x; x+=(); typeset -p x'
    typeset -a x=( '' )
    % zsh -fc 'typeset x; typeset -a x; typeset -p x'
    typeset -a x=(  )

Roman.


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

* Re: zsh adds empty string to local parameter when += is used
  2021-01-20 16:16 zsh adds empty string to local parameter when += is used jamil bio
  2021-01-20 16:22 ` Roman Perepelitsa
  2021-01-20 16:26 ` Peter Stephenson
@ 2021-01-20 16:31 ` Daniel Shahaf
  2021-01-20 16:40   ` jamil bio
  2 siblings, 1 reply; 6+ messages in thread
From: Daniel Shahaf @ 2021-01-20 16:31 UTC (permalink / raw)
  To: jamil bio; +Cc: zsh-workers

jamil bio wrote on Wed, Jan 20, 2021 at 13:16:54 -0300:
>         echo "${#files[@]} -- ${files[@]}"
> 
> 2 --  file1
> 3 --  file1 file2
> 4 --  file1 file2 file3

For future reference, there's «typeset -p files» and «print -r -- "${(q)files}"».


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

* Re: zsh adds empty string to local parameter when += is used
  2021-01-20 16:31 ` Daniel Shahaf
@ 2021-01-20 16:40   ` jamil bio
  0 siblings, 0 replies; 6+ messages in thread
From: jamil bio @ 2021-01-20 16:40 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-workers

Thanks Peter S for your amazing explanation. That is enough for me.

I had given up on getting an explanation but that is excellent news.

I will be using `local -a` and your other suggestions in the future.

Cheers,

J

On 20/01/2021 13:31, Daniel Shahaf wrote:
> jamil bio wrote on Wed, Jan 20, 2021 at 13:16:54 -0300:
>>          echo "${#files[@]} -- ${files[@]}"
>>
>> 2 --  file1
>> 3 --  file1 file2
>> 4 --  file1 file2 file3
> For future reference, there's «typeset -p files» and «print -r -- "${(q)files}"».


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

end of thread, other threads:[~2021-01-20 16:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-20 16:16 zsh adds empty string to local parameter when += is used jamil bio
2021-01-20 16:22 ` Roman Perepelitsa
2021-01-20 16:26 ` Peter Stephenson
2021-01-20 16:29   ` Roman Perepelitsa
2021-01-20 16:31 ` Daniel Shahaf
2021-01-20 16:40   ` jamil bio

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