From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21894 invoked from network); 30 Jan 2003 17:12:43 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 30 Jan 2003 17:12:43 -0000 Received: (qmail 1841 invoked by alias); 30 Jan 2003 17:09:54 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 5846 Received: (qmail 1832 invoked from network); 30 Jan 2003 17:09:54 -0000 Received: from localhost (HELO sunsite.dk) (127.0.0.1) by localhost with SMTP; 30 Jan 2003 17:09:54 -0000 X-MessageWall-Score: 0 (sunsite.dk) Received: from [192.11.222.163] by sunsite.dk (MessageWall 1.0.8) with SMTP; 30 Jan 2003 17:9:53 -0000 Received: from nwsgpa.ih.lucent.com (h135-1-121-22.lucent.com [135.1.121.22]) by ihemail2.firewall.lucent.com (Switch-2.2.2/Switch-2.2.0) with ESMTP id h0UH9tJ00658 for ; Thu, 30 Jan 2003 12:09:55 -0500 (EST) Received: from nwsgpb.ih.lucent.com by nwsgpa.ih.lucent.com (8.8.8+Sun/EMS-1.5 sol2) id LAA07762; Thu, 30 Jan 2003 11:08:53 -0600 (CST) Received: by nwsgpb.ih.lucent.com (8.11.6+Sun/EMS-1.5 client sol2) id h0UH8qp29390; Thu, 30 Jan 2003 11:08:52 -0600 (CST) From: kwong@nwsgpa.ih.lucent.com Message-Id: <200301301708.h0UH8qp29390@nwsgpb.ih.lucent.com> Subject: a calculator for zsh - zcalc and pcal conflict To: zsh-users@sunsite.dk Date: Thu, 30 Jan 2003 11:08:52 -0600 (CST) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit 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 > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~