From: kwong@nwsgpa.ih.lucent.com
To: zsh-users@sunsite.dk
Subject: a calculator for zsh - zcalc and pcal conflict
Date: Thu, 30 Jan 2003 11:08:52 -0600 (CST) [thread overview]
Message-ID: <200301301708.h0UH8qp29390@nwsgpb.ih.lucent.com> (raw)
All,
Earlier I posted a calculator "pcal" written in perl as
a followup to Clifford's post of "zcalc" written in zsh.
Andy Spiegl noticed a problem and his email is attached
below.
After some discussion, we figured out that this is due to
a conflict between the way "zcalc" and "pcal" uses the
variable "ans".
When "zcalc" is called, it implicitly typeset the variable
"ans" to -E (float). Once that is done, it is irreversible
until you "unset ans". And if you then use "pcal", it will
bomb if "ans" is set to a non-numerical value.
Some ways to work around it (none is ideal):
* refrain from using "zcalc" and then "pcal".
* "ans" for "zcalc" and "an" for "pcal" (you have to
modify the definition of pcal.s2 to change all "ans" to
"an".
mk
********************************************************************
Andy wrote:
>
> Hi Kwong,
>
> great stuff too!
>
> > Let me know if you find any bugs.
> I'm not sure if it's a bug, but it's strange and I don't understand why.
> It seems to have to do with the calculator of Clifford, because this
> happens:
>
> condor:~>pcal h abc * d
> ans = 8b8c
>
> condor:~>zcalc '4 * 4'
> zcalc: bad math expression: operator expected at `b8c'
> zsh: exit 1
>
> condor:~>unset ans
>
> condor:~>zcalc '4 * 4'
> 16
>
> condor:~>pcal h abc * d
> pcal-ans:4: bad math expression: operator expected at `b8c'
> zsh: exit 1
>
> Can you see why the two interfere??? $ans only holds a number, right?
> Besides I'm not even using ans in this example. Strange.
>
> So I turned on debugging (see below) and get this:
> condor:~>print $ans
> 3
>
> condor:~>pcal h abc * d
> trace pcal-ans:2(if)> [[ -n h abc * d ]]
> trace pcal-ans:4(then)> _x=trace pcal-ans:4(then cmdsubst)> echo h abc * d
> trace pcal-ans:4(then)> _x=trace pcal-ans:4(then cmdsubst pipe)> sed s/ans/3/g
> trace pcal-ans:4(then)> _x=h abc * d
> trace pcal-ans:5(then)> ans=trace pcal-ans:5(then cmdsubst)> pcal.pl h abc * d
> trace pcal-ans:5(then)> ans=8b8c pcal-ans:5: bad math expression: operator expected at `b8c'
>
> I don't get it, sorry.
>
> Okay, I've saved your script as pcal.pl and put this into my .zsh/alias:
>
> pcal-ans() {
> set -x
> if [[ -n "$*" ]]
> then
> _x=`echo $* | sed "s/ans/$ans/g"`
> ans=`noglob pcal.pl $_x`
> print ans = $ans
> unset _x
> else
> pcal.pl
> fi
> set +x
> }
> alias pcal='noglob pcal-ans'
>
> # ----------------------------------------------------------------------
> # Here are some quick calculators that output in integer
> # hexadecimal, decimal, and binary.
> zcalc () { print $(( ans = ${@:-ans} )) }
> zcalch () { print $(( [#16] ans = ${@:-ans} )) }
> zcalcd () { print $(( [#10] ans = ${@:-ans} )) }
> zcalco () { print $(( [#8] ans = ${@:-ans} )) }
> zcalcb () { print $(( [#2] ans = ${@:-ans} )) }
>
> # A key binding that will allow you to quickly get into zcalc
> bindkey -s '\C-xd' "zcalc "
>
> # this last one lets you calculate the ascii value of a single character
> zcalcasc () { print $(( [#16] ans = ##${@:-ans} )) }
> # -------------------------------------------------------------------end
>
>
> Any help would be very much appreciated!
> Thanks,
> Andy.
>
> --
> http://peru.spiegl.de Our project
> http://radiomaranon.org.pe Radio Marañón, Jaén, Perú
> o _ _ _
> ------- __o __o /\_ _ \\o (_)\__/o (_) -o)
> ----- _`\<,_ _`\<,_ _>(_) (_)/<_ \_| \ _|/' \/ /\\
> ---- (_)/ (_) (_)/ (_) (_) (_) (_) (_)' _\o_ _\_v
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
next reply other threads:[~2003-01-30 17:12 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-01-30 17:08 kwong [this message]
2003-01-30 17:34 ` Peter Stephenson
2003-01-31 12:03 ` Clifford Caoile
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=200301301708.h0UH8qp29390@nwsgpb.ih.lucent.com \
--to=kwong@nwsgpa.ih.lucent.com \
--cc=zsh-users@sunsite.dk \
/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).