* DWIM interface to date args; reverse array
@ 2017-11-15 14:37 ` Emanuel Berg
2017-11-15 14:57 ` Peter Stephenson
0 siblings, 1 reply; 3+ messages in thread
From: Emanuel Berg @ 2017-11-15 14:37 UTC (permalink / raw)
To: zsh-users
time-from () {
local from=$1
local to=${2:-now}
local -a order
### from
local -a from_items
from_items=(${(s:-:)from})
# instead of doing this one could reverse the original data array
# I Googled "zsh reverse array" and found ${(Oa)array}
# but seems to only work when I echo it,
# not assign it to another array
(( $from_items[1] < $from_items[3] )) && order=(d m y) || order=(y m d)
local from_y=$from_items[${order[(i)y]}]
local from_m=$from_items[${order[(i)m]}] # always 2
local from_d=$from_items[${order[(i)d]}]
from=${from_y}-${from_m}-${from_d}
### to
if [[ $to != now ]]; then
local -a to_items
to_items=(${(s:-:)to})
(( $to_items[1] < $to_items[3] )) && order=(d m y) || order=(y m d)
local to_y=$to_items[${order[(i)y]}]
local to_m=$to_items[${order[(i)m]}] # ditto
local to_d=$to_items[${order[(i)d]}]
to=${to_y}-${to_m}-${to_d}
fi
### compute
local ymd
local day
ymd=$(dateutils.ddiff $from $to -f '%Yy %mm %dd')
day=$(dateutils.ddiff $from $to -f 'total %dd')
### output
echo "${ymd// 0(y|m|d)/} ($day)"
}
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: DWIM interface to date args; reverse array
2017-11-15 14:37 ` DWIM interface to date args; reverse array Emanuel Berg
@ 2017-11-15 14:57 ` Peter Stephenson
2017-11-15 15:56 ` Emanuel Berg
0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2017-11-15 14:57 UTC (permalink / raw)
To: zsh-users
On Wed, 15 Nov 2017 15:37:44 +0100
Emanuel Berg <moasen@zoho.com> wrote:
> time-from () {
I'd suggest if the question is really "how do I reverse an array?", it's
best not to add distracting material which is simply hiding your
question. The more obvious the question is, the more people will bother
thinking about it.
On the other hand, if you were asking "how do I write this function
better?" then the context would certainly be important. But I don't
see that question here.
> # instead of doing this one could reverse the original data array
> # I Googled "zsh reverse array" and found ${(Oa)array}
> # but seems to only work when I echo it,
> # not assign it to another array
You haven't shown what you expected to work that didn't, so I can't
really answer what you actually said, so I'm reduced to guesswork.
Please show (just) what you think should work and doesn't. (Or, to
put it another way, the answer to what you just said is "yes, it does
actually work", which I'm guessing isn't going to help you much...)
Try
array=(${(Oa)array})
remembering the outer parenthesis, otherwise you are assigning a scalar.
pws
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: DWIM interface to date args; reverse array
2017-11-15 14:57 ` Peter Stephenson
@ 2017-11-15 15:56 ` Emanuel Berg
0 siblings, 0 replies; 3+ messages in thread
From: Emanuel Berg @ 2017-11-15 15:56 UTC (permalink / raw)
To: zsh-users
Peter Stephenson wrote:
> array=(${(Oa)array})
>
> remembering the outer parenthesis, otherwise
> you are assigning a scalar.
Aha, that's it! Cool.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-11-15 15:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <CGME20171115143925epcas2p39a7bfefe245b1b93d1d19dcf210770f3@epcas2p3.samsung.com>
2017-11-15 14:37 ` DWIM interface to date args; reverse array Emanuel Berg
2017-11-15 14:57 ` Peter Stephenson
2017-11-15 15:56 ` Emanuel Berg
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).