CONST value from STRING?
BlitzMax Forums/BlitzMax Programming/CONST value from STRING?
| ||
Is there any simple way to convert a string into its CONST equivalent. For example, if I read in from a text file the string "COLOR_RED" and I have an existing Constant COLOR_RED:int = 15, is there any way to tell the code that the string "COLOR_RED" is supposed to mean the constant COLOR_RED? Thanks in advance! :) |
| ||
Darn it- wrong forum...meant to go in regular BlitzMax Programming! |
| ||
No. Constants can't be changed, hence the name. Use a global instead. |
| ||
Yes, you have an array with the names of the constants, and you loop throug them until your string (in this case COLOR_RED" is found. That would be element 16 (For examlple), then you look at element 16 of your "Array of constants" and at element 16 it finds the value 15. @Gfk, I dont think changeing the value was the question here. |
| ||
Sorry...I didn't make myself clear. I don't want to change a Constant. Okay, let's say I have this constant... Const COLOR_RED:int = 15 Now, if my code reads data in a comma-seperated text file, and one of the fields that is read in says "COLOR_RED", then I have a string called "COLOR_RED". ...what I want is a variable to equal the '15' - the Constant value of COLOR_RED...I don't need that string "COLOR_RED". |
| ||
H&K ... yes, you got it! The only problem is that I have LOTS of constants...I was just hoping that someone out there had a better solution than looping through lists of string-equivalents for the CONST value. It would be really awesome if BlitzMax had a built in function to find a Constant value from a string that matches the Const name. |
| ||
Once you run a program, blitz doesn't know what your variable names are anymore. Even if it did it would have to use the same kind of search code that you are using. You can always speed it up by separating the list of strings into a separate list for each string-length, or you could read up about hash tables. |
| ||
@It would be really awesome if BlitzMax had... I have to agree totaly with Curtasic here, the built in code would just be the same sort of code that you would write, except yours would be more dedicated to exactly the problem you are what want solved. I do think this is a demic problem here, (I suffer from it as well), this beleif that Skid or Mark can solve my problem better than I can. If its messing with the core compiler then ok fair enough, but if its an addition to the moduals, then I have just as much chance if not more of being able to write the extra core commands that I want, because I know exactly what I require of them |
| ||
Agreed. I was just hoping that someone had a more elegant solution that what I was already using, or that I wasn't missing some built-in function (as it has happened before!). Thanks guys! |
| ||
For String - Value assignements in a datastructure which can efficiently be accessed, use BRL.MAP Thats key based set with log(n) (n amount of keys) average time on search, delete and insert, which is nearly the best you can get on such a data structure. |
| ||
For String - Value assignements in a datastructure which can efficiently be accessed, use BRL.MAP ..except in the case mentioned previously, the Int value would have to be wrapped up in its own Type. But generally, Maps are extremely useful for this kind of "mapping" requirement. |
| ||
Tachyon: I guess what you´re looking for is an enum {} datatype as C# has it, or an & operator as Clipper and Xbase++ offer it (in Xbase++, &string maps to a variable with the same name). BlitzMax does not seem to have something like that on the language level, so you must implement the mapping yourself. |