Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: TIfanx1999 on January 24, 2010, 02:52:30 am

Title: Compare getkey to a list
Post by: TIfanx1999 on January 24, 2010, 02:52:30 am
It's been quite a while since I've done any programming, but I seem to remember that there was a way to use the cumSum or seq( command to compare the keypress values in getkey to a list of elements. Can anyone confirm/deny this and post some code if it's possible?
Title: Re: Compare getkey to a list
Post by: {AP} on January 24, 2010, 03:38:41 am
Hm... I think I know what code you're talking about.
Though, I have to ask you to elaborate a bit. Technically, all you need to compare a keypress to a list is to type "getkey→K:K=<insert a list here>".

So, because of that, I need to know something like what you'll use this for then I can probably give you example code for that and explain it, if need be.
Title: Re: Compare getkey to a list
Post by: TIfanx1999 on January 24, 2010, 03:46:39 am
It was something along the lines of:

Repeat getkey=cumSum(24,25,26,34
End
Supposedly it was the fastest way to compare a keypress to a list of numbers instead of doing something horribly inefficient like:

Repeat getkey= 24 or getkey=25 or getkey=26 or getkey=34
End
Title: Re: Compare getkey to a list
Post by: {AP} on January 24, 2010, 03:56:34 am
Well, for that, it'd just be...

Repeat sum(getKey={24,25,26,34
End

For example, if getKey was 25 the "getkey={24,25,26,34" part would output "{0,1,0,0}" then the "sum(" would add the elements together to equal "1" or "TRUE".

If it was cumSum then it'd output "{0,1,1,1}" in the end, which isn't a boolean as required by the Repeat function.
Title: Re: Compare getkey to a list
Post by: TIfanx1999 on January 24, 2010, 04:07:35 am
It works, thanks! =D I wonder why i was remembering cumSum I dunno, it was something about a discussion of doing this with the best possible speed....I'm probably just remembering wrong. I sure am rusty XD
Title: Re: Compare getkey to a list
Post by: {AP} on January 24, 2010, 04:09:09 am
Haha, no problem!

I'm pretty rusty myself.
Had to actually test it on my calc first just to make sure I gave you the right information. ^^

(And I'm not sure that it's the fastest... but it's a hell of a lot faster and smaller than typing out "getKey=#" for each on you want to check. =P )
Title: Re: Compare getkey to a list
Post by: TIfanx1999 on January 24, 2010, 04:46:21 am
After some looking I found the routines I was originally trying to remember. The original did indeed use sum, (not cumSum) but the one that was recommended as being the fastest was with the max command.
IE:

Repeat max(getKey={24,25,26,34
End
Title: Re: Compare getkey to a list
Post by: ztrumpet on January 24, 2010, 10:57:04 am
Remember that if you want to use the keypress you'd need to do this:

Repeat max(Ans={24,25,26,34
getKey->K //You don't need to store it to K, but I usually do. :)
End
Title: Re: Compare getkey to a list
Post by: Silver Shadow on January 24, 2010, 11:40:28 am
Hmmm... I think I'll use this code... I've never thought of doing the keypress detection that way...
Thanks for helping me discover something new!
Title: Re: Compare getkey to a list
Post by: TIfanx1999 on January 24, 2010, 05:21:44 pm
@Ztrumpet, well yea that should go without saying. :) Funfact: The convention of using K for getkey started because that was what the example in TI's manual used. The same goes for using the variable I as the default variable in For loops.  ;D

@Silver Shadow:Great! It's a quick and simple way to do it. =D
Title: Re: Compare getkey to a list
Post by: ztrumpet on January 24, 2010, 06:37:37 pm
The convention of using K for getkey started because that was what the example in TI's manual used.
Ah, I think that's why I use it.  I never realized that. ;D
Title: Re: Compare getkey to a list
Post by: {AP} on January 24, 2010, 06:55:30 pm
Funfact: The convention of using K for getkey started because that was what the example in TI's manual used.

Really? I just used it because to me K = Keypress. I do that with variables a lot. Like Position in a menu = M or any temporary variable = X.
In fact... I never even read the TI manual. =D

Though, you're probably right for the most part. I'm always a weird exception it seems. =P
Title: Re: Compare getkey to a list
Post by: Builderboy on January 24, 2010, 07:27:46 pm
Ha, I never looked at the manual eaither, and I use K for all my key variables XD

I use F for all my for loop vars tho O.O and G too if I have nested for loops XD
Title: Re: Compare getkey to a list
Post by: TIfanx1999 on January 24, 2010, 07:50:21 pm
Alot of us (way back in the day) used the thick paperback manuals that TI used to include with their products as a stating point for programming and as a reference. Many people learned from looking at others code, and as time passed people wrote their own tutorials and used what they were familiar with as reference. Thus certain conventions were established regardless of them having any relevance. Although, I suppose "K" for key does actually make sense. I just find it interesting that some of these things have largely stayed the same since about 98/99.
Title: Re: Compare getkey to a list
Post by: DJ Omnimaga on January 24, 2010, 08:21:05 pm
I always used Z for key variable and For( loops. When a variable conflict occured I just used Theta for For( loops instead
Title: Re: Compare getkey to a list
Post by: Galandros on January 25, 2010, 09:37:05 am
I use theta and I for loops.
K for getkey, of course. I didn't know of TI examples... I just followed others.

I also use F for flags (1 or 0).

The lists I prefer to use is not L1 and L2 but L5 and L6 because is more friendly to user. You rarely use them but L1 and L2 are the first used, I suppose.
Title: Re: Compare getkey to a list
Post by: Eeems on January 25, 2010, 11:51:08 am
I usually will use K for getkey, M for menu output. A and B are my vars that I use most, along with X Y C D E F in no particular order. Although I will usually use x and Y for location variables.
Title: Re: Compare getkey to a list
Post by: DJ Omnimaga on January 25, 2010, 02:17:21 pm
The only bad part about everyone using K for example is that sometimes there are people who think K is the only variable that can be used with GetKey XD
Title: Re: Compare getkey to a list
Post by: Builderboy on January 25, 2010, 02:24:39 pm
You know what?  Now that I thing about it,  I might have used theta as my key variable up until I found donut quest.  I totaly picked apart the source :P and so many of my games were inspired one way or anoher by different parts of DQ.  Ha, even portal was a partial brainchild of DQ XD
Title: Re: Compare getkey to a list
Post by: DJ Omnimaga on January 25, 2010, 02:32:10 pm
You one what?
I do not understand this question.
Title: Re: Compare getkey to a list
Post by: Builderboy on January 25, 2010, 02:51:12 pm
Whoops,  sorry about that.  I'm on my itouch so a simple misspelling and it autocorrects to something else:P
Title: Re: Compare getkey to a list
Post by: Eeems on January 25, 2010, 02:53:38 pm
lol, I hate that x.x
hmm, too bad I'm studying for bio, I want to play a game of eliminate x.x
Title: Re: Compare getkey to a list
Post by: ztrumpet on January 25, 2010, 03:34:14 pm
The variables I use most are A, B, C, S, T, and K.  The only variable I'll always use for something is K, so my code is probably the messyist. :P
Title: Re: Compare getkey to a list
Post by: Galandros on January 25, 2010, 04:32:59 pm
Other things I remembered: I also use theta as a "temporary" variable through all code, this means, it is used once and can be destroyed later for other function.
And I have seen "I" variable used in loops in other languages like JavaScript (but in lowercase). For nested loops, "j" is used next. :P

Also X and Y are good for position but some graph functions zeroes Y or X. So I use A and B or R and C (row and column).

I try to use a variable letter as the starting name for its function. For example, "H" for health, "J" for jump, "M" for menu or magic, etc..

Oh we are offtopic but is interesting to see there is the K variable convention.
Title: Re: Compare getkey to a list
Post by: ztrumpet on January 25, 2010, 05:11:13 pm
And I have seen "I" variable used in loops in other languages like JavaScript (but in lowercase). For nested loops, "j" is used next. :P
It's funny;  In C++ I use i, j, and k for my for loops, but in Ti Basic I use A, B, and C.  I'm wierd! :P
Title: Re: Compare getkey to a list
Post by: miotatsu on January 31, 2010, 10:13:46 pm
i think of I=Increment for loops, and thus the reason i use it in for loops :{O
i'm kind of leaning towards the approach of just using variables as i get to them in order, and saving a table defining what each variable is used for when, i think this would actually be a cleaner approach because when you get to the point where every single variable is in use/has multi-uses it begins to get very confusing and you can forget what does what so you have to create tables anyway
Title: Re: Compare getkey to a list
Post by: jsj795 on January 31, 2010, 10:27:37 pm
I actually use theta for getkey, and Z for repeat group, acting something like flag.
A,B for variables I use a lot. X,Y for position (sometimes not because Y tend to change when using ClrDraw)
Title: Re: Compare getkey to a list
Post by: rthprog on February 25, 2010, 12:20:00 pm
Repeat max(Ans={24,25,26,34
getKey->K //You don't need to store it to K, but I usually do. :)
End

Repeat max(Ans={24,25,26,34
getKey
End
Ans-->K

Takes up an extra byte, but ought to be a bit faster
Title: Re: Compare getkey to a list
Post by: Eeems on February 25, 2010, 01:27:47 pm
hmm, interesting. I'll have to remember that.
also welcome to Omnimaga rthprog.
Title: Re: Compare getkey to a list
Post by: rthprog on February 25, 2010, 03:31:55 pm
@Eeems_
thanks!  Omnimaga's TIBasic subforum seems a bit more active than Cemetech's (excluding John Massey, of course)
Title: Re: Compare getkey to a list
Post by: Eeems on February 25, 2010, 03:50:11 pm
np
yeah it does, we have more basic programmers here that are active.
Title: Re: Compare getkey to a list
Post by: DJ Omnimaga on February 25, 2010, 04:26:14 pm
Hi rthprog, for some reasons when I saw your nickname on latest members I was certain you alerady signed up here before so I thought to myself: "oh crap, I accidentally deleted him in a spambot account purge x.x", but then I remembered it was Reapex who had an account. Anyway welcome here ^^

Activity-wise, we normally are more focused on calc game projects while TIBD/Cemetech are more help-oriented/non-game-oriented (and UTI an hybrid of all), but it seems there have been more activity in the help forums here lately, especially since we split TI-BASIC out of the only calc help subforum we had.
Title: Re: Compare getkey to a list
Post by: ztrumpet on February 25, 2010, 04:39:27 pm
Repeat max(Ans={24,25,26,34
getKey->K //You don't need to store it to K, but I usually do. :)
End

Repeat max(Ans={24,25,26,34
getKey
End
Ans-->K

Takes up an extra byte, but ought to be a bit faster

Great idea!  I'm sure this is faster when no key is pressed, but I don't know if it's faster when a key is pressed.  It's nice code, though. ;D
Edit: Oh, and because of the new-line it takes two more bytes.
Welcome here!  *ZTrumpet points rthprog towards the Introduce Yourself sub forum. :)
Title: Re: Compare getkey to a list
Post by: DJ Omnimaga on February 25, 2010, 04:42:12 pm
*ZTrumpet points rthprog towards the Introduce Yourself sub forum. :)
He'll be scared to death D:
Title: Re: Compare getkey to a list
Post by: Eeems on February 25, 2010, 05:09:32 pm
Lol yeah, that subforum is a scary place x.x that's why I don't venture there often.
Title: Re: Compare getkey to a list
Post by: ztrumpet on February 25, 2010, 05:10:49 pm
 *ZTrumpet lols at Eeems and DJ...
The only extremely scary part is the first page of DJ's topic...
Title: Re: Compare getkey to a list
Post by: DJ Omnimaga on February 25, 2010, 05:30:48 pm
And you must avoid the peanuts bags being thrown from everywhere
Title: Re: Compare getkey to a list
Post by: Builderboy on February 25, 2010, 07:14:24 pm
I'm sure this is faster when no key is pressed, but I don't know if it's faster when a key is pressed.

Yeah, i see this optimization a lot actually, but i tend to shy away from it.  I don't need speed when nothing is happening :P

Either way, welcome to Omnimaga! and look out for stray lobsters O.O
Title: Re: Compare getkey to a list
Post by: rthprog on February 25, 2010, 08:47:34 pm
Lol, okay.

@Builderboy
"I don't need speed when nothing is happening"

Good point.  In my defense, I'm somewhat used to leaving the "-->K" out altogether; often it is unnecessary to store the key at all.
Title: Re: Compare getkey to a list
Post by: Builderboy on February 25, 2010, 08:58:22 pm
That is very true :) That would be the fastest option.  Or you could be totally awesome like Weregoose and use nothing but Ans for the entire program ;D
Title: Re: Compare getkey to a list
Post by: ztrumpet on February 25, 2010, 08:58:36 pm
...I'm somewhat used to leaving the "-->K" out altogether; often it is unnecessary to store the key at all.
Right, and it's fastest if you don't have to store K. ;D

Edit: Ninjaed!
Title: Re: Compare getkey to a list
Post by: meishe91 on March 05, 2010, 01:46:34 am
Haha at the risk of actually being on topic about the movement part of this I remember seeing something about movement code at TI-BASIC Developer (http://tibasicdev.wikidot.com/movement-explanation). It is supposedly faster than usual and deals with ⌂List. I'm not sure how it works, with my lack of knowledge of those commands, but thought I'd throw that out there haha.