From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20013 invoked by alias); 26 Apr 2016 11:04:23 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 21497 Received: (qmail 20484 invoked from network); 26 Apr 2016 11:04:22 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 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== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=B109jbHoBTTqR2AoQWBVGq42NWpt3gMbUfxqSa+UZ7k=; b=RKmKBKac2oLGgT2K5j3kDnLUzrZr9JGIUwhMFSlJX5hvH+Zfe9vnjiJv5x5qMvL/Jm 1TuX4qZhVYaxUfhZft6Q264Lf+/vN7iwt5cY5C3jo093xfuVL/8PJlNUYdsloSu/kMZC B6VrK99rB2IbacRv7ZZVP+6Y6NvCvj15AjtR25Peb3O0CpbN7I+8oaxs+msZZmChRwtM F0q9k9hhYyE3EFgLNUOmYFop4Du9aZOmIkMuzjeRv+CLTYfgxS0P/NjA3inYckMMIace mG46DzP9kWZV7bjeuxi+5KyMBI03Y5qrBmw8CfGZsOV7Z6R8g6WJtV/azJX8uMn8smSy R4FA== X-Gm-Message-State: AOPr4FUJiy0WY9GcdydJl6AqMcdFhQX8DBGrfOuR9MUqdqc1by+pRf/REvV4v2/Ql4LtiNTYLzBZDkuL9v13/g== X-Received: by 10.112.204.5 with SMTP id ku5mr1033275lbc.31.1461668657544; Tue, 26 Apr 2016 04:04:17 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <160425124932.ZM7571@torch.brasslantern.com> References: <160425124932.ZM7571@torch.brasslantern.com> From: Sebastian Gniazdowski Date: Tue, 26 Apr 2016 13:03:58 +0200 Message-ID: Subject: Re: Most frequent history words To: Bart Schaefer Cc: Zsh Users Content-Type: text/plain; charset=UTF-8 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 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.