Variable Names - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Variable Names (/showthread.php?tid=2188) Pages:
1
2
|
Variable Names - Dimster - 11-19-2023 Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok. RE: Variable Names - RhoSigma - 11-19-2023 (11-19-2023, 06:18 PM)Dimster Wrote: Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok. https://qb64phoenix.com/qb64wiki/index.php/Variable Guess it's for historic reasons aka QB4.5 compatiblity. RE: Variable Names - CharlieJV - 11-19-2023 (11-19-2023, 06:18 PM)Dimster Wrote: Is there a reason why a variable name can not begin with a numerical value? ie a1 = 100 is ok but 1a = 100 or 1a$ = "100" is not ok. Not that I know a heck of a whole lot about interpreters/compilers parsing and tokenising (?) a program, but I imagine there's value in the process knowing immediately that:
I think. RE: Variable Names - James D Jarvis - 11-19-2023 It would make parsers explode. RE: Variable Names - Haggarman - 11-19-2023 It's a text parsing problem. Write your own compiler and see how difficult it becomes when what appears to be numbers at first are no longer numbers because it has some other non-number in it. Especially problematic for a language like BASIC where a typo can bring another variable into existence to cause a difficult to track down bug. RE: Variable Names - SMcNeill - 11-19-2023 If I had to attribute the choice to anything, it'd probably be simply to the way that VAL behaves. VAL("123abc") = 123 VAL("abc123") = 0 VAL takes all the numbers from the start and then assembles them into a valid numeric value, stopping whenever an unexpected character is found. If I have to venture a guess, I'd say the original creators of BASIC used some logic, based around this concept, to parse what makes a number and what could represent a keyword or variable. Since we tend to try and follow those some historical rules as what was set by QB45 and the folks previously, we don't tend to allow numbers before letters in variables either. Which reminds me of this old article of interesting fact: Quote:The Space Shuttle and the Horse's Rear End RE: Variable Names - bplus - 11-19-2023 Original Basic used single letters for variables and tended to that to save precious Ram space. That's my theory of where it all started, no cavemen involved sadly. RE: Variable Names - mnrvovrfc - 11-19-2023 To be able to use a variable name like "1a" we would all have to use "LET" again. Obligatorily. It's simply because the earliest versions of BASIC supported line numbers! Yes remember that? Couldn't have a program in GW-BASIC for example without line numbers. And in an 8-bit computer like my Radio Shack TRS-80 Color Computer 2, "1a=3" would be broken down to: Code: (Select All) 1 a = 3 The earliest BASIC's on 8-bit computers allowed almost all spaces omitted when composing programs because a space actually took up room in memory, and back then those computers had to work with 16KB or less RAM. Oh well C and Lua and many other programming languages also don't permit numerals to begin a variable name, for other reasons. Dimster why do you want to have a variable like "1a" anyway? RE: Variable Names - Jack - 11-19-2023 you could use Lisp (setq 1a 123) 123 (expt 1a 20) 628206215175202159781085149496179361969201 RE: Variable Names - DSMan195276 - 11-19-2023 To pile on to all the stuff others have already pointed out ( ), there are numeric literals that include alphanumeric characters, so allowing variables to start with numerals create pretty bad ambiguities even in QB45/QB64 where spaces are required around most things: Code: (Select All)
Of course, this could be fixed by simply not allowing you to use variable names that could be confused with a numeric literal, but: 1. That's a lot of extra work for little gain, simply banning variables from starting with a numeric is much simpler. 2. It would prevent us from adding any new numeric syntax's in the future if they would conflict with allowed variable names. There's probably some other conflicts that I haven't considered, but that's the most obvious to me. |