Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
a calculator for zsh - zcalc and pcal conflict
- X-seq: zsh-users 5846
- From: kwong@xxxxxxxxxxxxxxxxxxxx
- To: zsh-users@xxxxxxxxxx
- Subject: a calculator for zsh - zcalc and pcal conflict
- Date: Thu, 30 Jan 2003 11:08:52 -0600 (CST)
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
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
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Messages sorted by:
Reverse Date,
Date,
Thread,
Author