Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: A bug or improperly formatted script
- X-seq: zsh-users 22497
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: linuxtechguy@xxxxxxxxx, zsh-users@xxxxxxx
- Subject: Re: A bug or improperly formatted script
- Date: Sat, 25 Feb 2017 14:12:11 -0800
- Authentication-results: amavisd4.gkg.net (amavisd-new); dkim=pass (2048-bit key) header.d=brasslantern-com.20150623.gappssmtp.com
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=ODLLgZWSZWNc0yqGMwdMynCxT68GaGTZyjxGcmBabIs=; b=uHFNPNz5XjyQLnQJb/7kTTzjLvl3bxj4Z9/DYyDiLKsjvFGGZYQtthNWCIYDWbF2FN Fw7/hcL7elbjLSks9aPX/qZySqnsrw2ZtzqKrTdL7nLDbfQ0U54DKq0LUGVB3Vjws9BZ VH6Ls5UM6/w4vLTg/8ZjeOyDLrkNtc2uDxG1rpf4YEm0eCaMBHQbU5vriF3YJHCmk8y+ 2XR3IyOaPQ2oJdrJmM24DcHjwpePMS7ZCr1Y1ulxcnjYnTJvpnyKVWRZ7+HU+l7uwdCD gR0f+RUE4UemBhHlLtsxleg1Dl8hMgHTuCwrrVlD6w/ui4AQvubT63gTNnYMNd2iNKM5 kzsw==
- In-reply-to: <CA+rB6GJoCDbdEVYR-zYEh6P0Oau7VAxQeW6UcnksdeB3hvL=HQ@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CA+rB6GJoCDbdEVYR-zYEh6P0Oau7VAxQeW6UcnksdeB3hvL=HQ@mail.gmail.com>
On Feb 25, 12:34pm, Jim wrote:
}
} While doing a search, unrelated, I found the following on the
} zsh-lovers(1) man page:
}
} # Count the number of directories on the stack
} $ print $((${${(z)${(f)"$(dirs -v)"}[-1]}[1]} + 1)) #...
}
} I was surprised the first time I tried it I got "-2" as the output.
}
} It would appear as if the original script is returning
} a scalar when the stack only has one entry and an array when the
} stack has 2 or more entries.
Yes, this is a known / traditional behavior, and it somewhat regularly
comes up in examples where a program has been tested only in the "more
than one element" cases.
The problem is that nesting ${${...}} "wants" to treat the inner ${...}
as a scalar, and will do so unless something else forces it to be an
array. (f) doesn't accomplish that because $(dirs -v) produces only
one line of output with the final newline stripped, so there is nothing
for (f) to split on. (@) won't accomplish it because it only means
that things that are already arrays should remain so.
So what can be done to force interpretation as an array here? Unless
someone else has a better solution, the answer is to forcibly assign
the result to an array variable:
print $((${${(z)${(Af)reply::="$(dirs -v)"}[-1]}[1]} + 1))
Once we are expanding ${reply} rather than ${"$(dirs -v")} the array
property is preserved and [-1] remains an array subscript instead of
becoming a scalar string slice.
A follow-up to this is going to zsh-workers.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author