Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Functions for parsing and sorting roman numerals
- X-seq: zsh-users 16008
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Functions for parsing and sorting roman numerals
- Date: Mon, 9 May 2011 14:11:49 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=/gZd6CQp+Gf2zeXHN/c6/QcPhWRy94eY2Y2/RZfOVmc=; b=lHMH8nLzr9rjl5f3/S8+D0gtW3Sp/Cy7NpR5Ov6HDurq+r22AAqNDTSSaalAeBgey+ Y/hB77B2lOKjHkiU28baZYW5dCMZn4lGakEfqS4yg+nNJvuwbrDAJTtb4iNqD8xU8vr0 UptyUQGccnRZI0A4BcgQYrKmB1WNC/iBg6joA=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=uxdoeT1F29OmziSyJxw+CHTnSvIe0BnqdaQm3noYdeiMvfziX5CP5+4NJ9oFjUdIC/ abvBCX6ssBUMyagPpvielNF2xw3SH35r9JiAiV7UMNOqZ7pzVyiQOkceg//oaRMPJEep UjPLQ51w8mc7di24h7LMbqyMqRcnm+BxpLlh0=
- 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
I started fiddling and came up with this, it doesn't do much validation.
# pass a second arg to set result to that var, otherwise echo result
parseroman () {
local max=0 sum i j
local -A conv
conv=(I 1 V 5 X 10 L 50 C 100 D 500 M 1000)
for j in ${(Oas::)1}; do
i=conv[(e)$j]
if (( i >= max )); then
(( sum+=i ))
(( max=i ))
else
(( sum-=i ))
fi
done
if (( ${+2} )); then
: ${(P)2::=$sum}
else
echo $sum
fi
}
#delimits numerals by space, _, or start/end of filename, to protect
random letters in words
#here's hoping gmail won't munge this line
romansort() {
REPLY=${REPLY:gs/(#b)( |_|(#s))([IVXLCDM]#)(
|_|(#e))/'$match[1]$(parseroman ${match[2]})$match[3]'}
}
% parseroman MMXI
2011
% parseroman MCMLXXXIV
1984
% parseroman MMMC result
% echo $result
3100
% touch I II III IV V VI VII VIII IX X XI MMXI MCMLXXXIV MMMC
# default lexical sort
% print *
I II III IV IX MCMLXXXIV MMMC MMXI V VI VII VIII X XI
# lexical sort of values, not that useful ;)
% print *(o+romansort)
I X XI MCMLXXXIV II MMXI III MMMC IV V VI VII VIII IX
# numeric sort of values
% print *(no+romansort)
I II III IV V VI VII VIII IX X XI MCMLXXXIV MMXI MMMC
# and the actual reason I wrote it
% print -l *(no+romansort)
Album I with music
Album II with music
Album III with music
Album IV with music
Album V with music
Album VI with music
Album VII with music
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author