zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Most frequent history words
Date: Mon, 25 Apr 2016 12:49:32 -0700	[thread overview]
Message-ID: <160425124932.ZM7571@torch.brasslantern.com> (raw)
In-Reply-To: <CAKc7PVCmE-pnDQNCJWS91id7=L+x4M8+q5oLd076AcVgOYHf7A@mail.gmail.com>

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.


  reply	other threads:[~2016-04-25 19:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-25 10:36 Sebastian Gniazdowski
2016-04-25 19:49 ` Bart Schaefer [this message]
2016-04-26 11:03   ` Sebastian Gniazdowski
2016-04-26 19:39     ` Bart Schaefer
2016-04-27  9:14       ` Sebastian Gniazdowski
2016-04-27 15:59         ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=160425124932.ZM7571@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).