Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / getting values from strings

Author
Message
ruckertheron
7
Years of Service
User Offline
Joined: 12th Nov 2016
Location:
Posted: 7th Jul 2018 14:26
Is there a way to get the value from a variable through a string? Like if a$ = "handle" and handle = 7, is it possible to get handle from the a$?

Example:


a$ = handle
handle = 7


print_int(a$)


Is it possible to make a function named print_int() with one parameter as a string that will print 7?
puzzler2018
User Banned
Posted: 7th Jul 2018 15:29
where does 7 come from? - its not its length
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 7th Jul 2018 16:18
As far as I know you can't get the value of a variable from a string that is set equal to the name of said variable.
Zen Jones
6
Years of Service
User Offline
Joined: 1st Jul 2017
Location:
Posted: 7th Jul 2018 22:32 Edited at: 7th Jul 2018 22:35
I'm not sure I completely understand your query, but I'll give it a shot.
First issue with your code above is that code is executed left to right, top to bottom

Global handle = 7
a$ = str(handle)
print(a$)
You'll get 7 out.

now if you did
Global handle = 5
a$ = str(handle)
handle = 50
print(a$)
You'll get 5 out because the assignment happened after the string conversion.

Even if we replace the a$ with a numerical variable placeholder, the result is the same.
So if you did
handle = 3
myvar = handle
handle = 30
print(str(myvar))
you'd get 3 out.

So can you shift duplicate data from one variable to another? Yes. with or without data type shifts depending on how you do it. But what question exactly are you asking? Are you looking to use handle as an intermediary between the value and the string? you can just print the value directly using Print(str(handle)). Are you trying to get "handle" when you assign it to a$? Then look at the example below.

If there's a particular reason you need the data in that format, I suggest an array of a custom type.

In the array format, you can put all your global integers in the array with their name in the name field, and their value in the value field.
You'll have to search the array for the variable name in order to get the index number if you have a lot of global integers, or just type it in manually if you prefer.

Other than using an array that contains the variable name, If you're looking to get "handle" out of a$ then you need to assign "handle" to a$. ie a$ = "handle".

and in answer to your last question, yes. you can make a custom function to print stuff. However! unless the function is being called during every sync(), it'll likely flash by so fast you won't even see it.
ruckertheron
7
Years of Service
User Offline
Joined: 12th Nov 2016
Location:
Posted: 8th Jul 2018 15:01
I think you kind of misunderstood me.. but its totally my fault for I didn't explain it right.. So here is another attempt...

Lets say I have the variable HANDLE and I set it to 7. HANDLE = 7
Is there a way to get the value of that variable from a string? Without the str()? a$ = "HANDLE"
So If I make a function print_int( a$) that will return then value of HANDLE and not a$?




After thinking about it a little, I came up with something that might not be the best thing to do in that situation but I'm still brainstorming.

Create a conversion from letters to numbers where all possibilities of a word are stored at a particular number.

A - 1
B - 2
AA - 27 {A-Z is 26 then add character)
AB - 28
AZ - 56 {A in the tens place equals 26 + Z which equals 26
CZ - (26*3) + 26

AAA - (26 * 27) + 27

ZZ - (26*26) + 26

You get the picture... Problem is eventually the more characters the higher the numbers get..

Then it would store values in large arrays:

Values as string[3000000]

so the variable MAP would be stored at the VALUES index of ((13+26)*26)+16 which is 1030.
say I want MAP to equal 7.

so VALUES[1030] ="7"


I would be able to get 7 from MAP like this:


store("MAP=7")

where store() would store the string "7" into VALUES[1030] based on the conversion.

I can then get the value creating another function get("MAP") that will return "7"

But I might have to make it 2 dimensional so the 2nd dimension will determine what type of variable it is...

I know thats kind of confusing but maybe someone will understand it!
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 8th Jul 2018 15:32 Edited at: 8th Jul 2018 15:37
What you need is a map. Maps are data constructs where a key (such as "handle") is mapped to a value (such as 7). AppGameKit doesn't handle maps natively, but you can write your own map functions. The simplest way to do this would be to create two arrays, one for the key and one for the value, and then you can search the array and retrieve the corresponding value.

Now this method works ok if the number of keys are few, but can become quite slow if the arrays get large. You can optimize the maps by keeping the keys sorted and using a binary search to retrieve a value. Also you can create a type which contains both the key and value and set a single array to that type.

C++ has native support for maps. If you plan on using tier 2, you might want to look at that.

Edit: https://en.wikipedia.org/wiki/Associative_array more information on maps (or associative arrays as they are sometimes called) and some ways to implement them.
Zen Jones
6
Years of Service
User Offline
Joined: 1st Jul 2017
Location:
Posted: 8th Jul 2018 17:00 Edited at: 8th Jul 2018 17:05
I'm really confused at this point. I tried to follow along with your detailed description, but I guess a better question to help understand the query is to respond with another query, and I ask it realizing that TomToad may have already answered your question, But I have to know: What is the end goal? Are you developing a new kind of encryption? are you attempting to store data? are you working on solving room temperature fission?

I've read your query again and it made my head hurt less and less each time I re read it. I'm not sure if this will even help but...

Since you're doing something funky with letter subs.. A-Z = 1-26, You're adding nearby letters. or multiplying them?
But you seem to be inventing a rhombicuboctahedron number system. (I had to google polygon with 26 sides).
Here's the confusion...
ruckertheron wrote: "
A - 1
B - 2
AA - 27 {A-Z is 26 then add character)
AB - 28
AZ - 56 {A in the tens place equals 26 + Z which equals 26
CZ - (26*3) + 26

AAA - (26 * 27) + 27

ZZ - (26*26) + 26
"

AA = 27, so an A in the tens column = Z, so why not just ZA = 27, AZ = 27, AA = 27. If you're adding numbers next to each other as letters, Then you also get 27 with BY, CX, DW, EV. But if you're just doing substitution... AA = 11. AZ = 126, ZA = 261
Then you have CZ So instead of adding them, its not 3+26, its 26*3+26....
So then taking this back to AA, is really 26*1+1 = 27
And then extrapolating it from your example I get this....

Millions Place |Hundred Thousands|TenThousands|Thousands|Hundreds|Tens|Ones|
8031810176| 308915776| 11881376 | 456976| 17576| 676| 26

Why are you inventing a rhombicuboctahedron number system? There's got to be an easier way. I really hope I'm not doing your homework by providing the following code.
by the way, Millions place has just broken math in AGK.

ruckertheron
7
Years of Service
User Offline
Joined: 12th Nov 2016
Location:
Posted: 8th Jul 2018 17:13
@TomToad that was a great idea! Genius! I did something similar to this in DarkBasicPro where I made,set, and returned objects as names instead of numbers and assigned object names to groups whenever necessary. Thanks
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 9th Jul 2018 15:34
Quote: "I would be able to get 7 from MAP like this:


store("MAP=7")

where store() would store the string "7" into VALUES[1030] based on the conversion.

I can then get the value creating another function get("MAP") that will return "7"

But I might have to make it 2 dimensional so the 2nd dimension will determine what type of variable it is...
"


What you described is an associative array, or Map as Tom stated. (or dictionary I guess in some languages)

If you write your own Map functions, I'd create a key-value UDT rather than rely on parallel arrays. Then you can keep your map sorted by key and use a binary search for lookup.

Generally, your array would only hold values of the same type. Some languages allow mixed data types but they also contain commands for determining the type of data, typeof.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-03-29 15:33:14
Your offset time is: 2024-03-29 15:33:14