Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Most frequent history words
- X-seq: zsh-users 21497
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: Re: Most frequent history words
- Date: Tue, 26 Apr 2016 13:03:58 +0200
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=B109jbHoBTTqR2AoQWBVGq42NWpt3gMbUfxqSa+UZ7k=; b=j5MrZBS0EtqK/6U2LbgCalvdz8jBUrLUvwIRHsCtknk3/Mc12ND26LGgfvGY/yrVZJ zHfgBQurtFZPXV+607E32nLYR2Em3kybkH/m9feEViP8QAyfr89ZpwF6dJVOVKTDpuUz EPPbcKOAQ0VpG6pUJ8y8Qe80fyoPstkH2A4MAfzMWit1k+d4u2VfJ2Ml3YWS5loWJCYp zH7zfOGq/xvI9l/BOSJun8XcKqCLITFPy9O6+wdJd5TUwuchqYvwTyNIyf0cug1CJVQI guspGvJSjiLFt0a8CWnP2cA+tq5iyJqqcT8F3GPvy8hNBOWTik3a53VcdDH+1DLsz3d4 vCXQ==
- In-reply-to: <160425124932.ZM7571@torch.brasslantern.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: <CAKc7PVCmE-pnDQNCJWS91id7=L+x4M8+q5oLd076AcVgOYHf7A@mail.gmail.com> <160425124932.ZM7571@torch.brasslantern.com>
The code works. Thanks. Had only to change (on) to (On). Interesting
trick with the sorting on "$v=$k". Here is a complete version for
someone to quickly reuse:
typeset -A uniq
for k in ${historywords[@]}; do
uniq[$k]=$(( ${uniq[$k]:-0} + 1 ))
done
vk=()
for k v in ${(kv)uniq}; do
vk+="$v=$k"
done
print -rl -- ${${${(On)vk}#<->=}[1,10]}
Interesingly, changing ${historywords[@]} to ${history[@]} and
"${history[@]}" doesn't change script's output, it still outputs most
frequent words, not history entries.
Best regards,
Sebastian Gniazdowski
On 25 April 2016 at 21:49, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Apr 25, 12:36pm, Sebastian Gniazdowski wrote:
> } Subject: Most frequent history words
> }
> } Hello,
> } can the following be made less coreutils dependent, i.e. more pure-Zsh code?
> }
> } print -rl "${historywords[@]}" | sort | uniq -c | sort -k1,1nr -k2,2 | head
>
> It can, but it's probably not very efficient.
>
> The pipe to sort can be replaced with
>
> print -rl -- ${(o)historywords[@]}
>
> The "uniq -c" would have to be replaced by a loop building a hash whose
> keys are words and whose values are the count thereof (making the initial
> sort irrelevant).
>
> typeset -A uniq
> for k in ${historywords[@]}
> do uniq[$k]=$(( ${uniq[$k]:-0} + 1 ))
> done
>
> Some quoting on $k such as ${(b)k} is probably required there, this is
> the shakiest part of the process.
>
> Then the final "sort -k..." would have to be done by iterating over the
> hash, with "head" just taking an array slice.
>
> vk=()
> for k v in ${(kv)uniq}
> do vk+="$v=$k"
> done
> print -rl -- ${${${(on)vk}#<->=}[1,10]}
>
> Plus unwrapping there whatever quoting on $k you did in the first loop.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author