|
Post by sarossell on Jan 14, 2020 15:37:06 GMT -5
Way back in the 80s, I had a Sinclair ZX81 with only 1K of RAM. I would often see coding tricks to conserve memory. One of them I thought was particularly clever was a method to avoid having to use an IF..THEN statement by embedding logical decisions in a string concatenation. But for the life of me, I can't remember how it worked, and I have yet to find it again in my old magazines. Anybody know how to do this?
The code would look SOMETHING like this (NOTE: This code does NOT work.):
PRINT ((Encrypted = true) + "En" OR (Encrypted = false) + "De") + cryption complete."
The idea is that the logical decision would determine which prefix ("En" or "De") to prepend to the message ("cryption complete.").
The closest I got to a working solution in LB is:
notice mid$("DeEn", (Encrypted = true) * 2 + 1, 2) + "cryption Complete."
But this isn't quite as "clever" as I remember.
:@)
|
|
|
Post by tsh73 on Jan 15, 2020 3:37:04 GMT -5
Me never had ZX81 - and memories of clone of ZX Spectum long faded - but I googled ZX Spectrum Manual and found this:
You can also make string valued conditional expressios, but only using AND
x$ AND y has the value x$ if y is non-zero "" if y is zero
so it means x$ if y (else the empty string).
But I used to IIF(condition, yesCase, noCase) from Visual Basic so I made these functions
'example: print iif(2>3, 10,20) 'Numeric result. Check fails - Prints 20 print iif$(2<3, "10","20") 'String results. Check holds - Prints 10 'some more examples x = rnd(1)-0.5 y = sqr(iif(x>0,x,0)) 'in the expression we resrict argument to be >=0 print x, y print "X is ";iif$(x>=0, "positive","negative")
'------------------------------------------------ function iif(test, valYes, valNo) iif = valNo if test then iif = valYes end function
function iif$(test, valYes$, valNo$) iif$ = valNo$ if test then iif$ = valYes$ end function
But! there is a WORD$ function that could do some neat things, like
true=1 'source, using mid$ for Encrypted = 0 to 1 print mid$("DeEn", (Encrypted = true) * 2 + 1, 2) + "cryption Complete." next print
'word$ with any condition for Encrypted = 0 to 1 print word$("De En", (Encrypted = true) + 1) + "cryption Complete." next print
'word$ with boolean variable (0..1) for Encrypted = 0 to 1 print word$("De En", Encrypted + 1) + "cryption Complete." next
|
|
|
Post by sarossell on Jan 15, 2020 6:36:11 GMT -5
Ah! Many thanks! I particularly like the WORD$ approach.
|
|
|
Post by mayhew61 on Jan 13, 2021 22:24:58 GMT -5
This is neither groundbreaking, or particularly relevant, but i had the Apple2e ages ago, your question made me think of it.
You're making this too hard. Try:
' a=0 for encryption, 1 for decryption y$="EnDe" print mid$(y$,2*a+1,2);"cryption complete."
Applesoft had an even more clever trick. you could use a logical expression as a 0 or 1 in a math expression. Like this:
max=((a>b)*a)+((b>a)*b)
The upshot being that the larger number would be multiplied by 1 and added to 0, returning the larger number in one statement. Anyway, thanks for the trip down memory lane!
|
|