Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct_get_by_index or struct_get_values & accesor #8578

Closed
legocris opened this issue Dec 1, 2024 · 4 comments
Closed

struct_get_by_index or struct_get_values & accesor #8578

legocris opened this issue Dec 1, 2024 · 4 comments
Labels
feature request New feature (or a request for one)

Comments

@legocris
Copy link

legocris commented Dec 1, 2024

Is your feature request related to a problem?

I would like to access with ease to struct members by index. Just that.

An example where I need this:
When getting values from an internet database, sometimes I get nested structs like this:

struct = {first: {second : 10}}
//So to get the value I have to do this
var _names = struct_get_names(struct);
var _nested = struct[ $ _names[0] ];
value = _nested[ $ struct_get_names( nested )[0] ];

Describe the solution you'd like

To add one or both of the next functions.
struct_get_by_index(struct, index) => Gets "index" element of the struct (ordered by default way) .
struct_get_values(struct) => Similar to struct_get_names except it rater returns the values on the array.

It would be much cool if you add a way to access to those values by index on an accessor but I don't know the implications of having a single data structure with multiple accessor types.

Describe alternatives you've considered

I currently do this if it can give you an idea how could an accessor of the kind be used

//Adds __names & __values fields to structs
function struct_indexer(_struct){
	static _recursive = function(__value, __recursive){
		if ( is_struct(__value) ){
			with{ __recursive }
				struct_foreach( __value, function(__n, __v){__recursive(__v, __recursive)});
			
			var __names = struct_get_names(__value);
			var __values;
			with {__value} 
				__values = array_map(__names, function(__v){ return __value[$ __v] } );
			struct_set( __value, "__values", __values);
			struct_set( __value, "__names", __names );
		}
		else if ( is_array(__value) ) 
			with{ __recursive }
				array_foreach( __value, function(__v){__recursive(__v, __recursive)});
	}
	
	_recursive(_struct, _recursive);
}

struct = {first: {second : 10, second2 :[1,4,5]}};

show_debug_message( struct.__values[0].__values[1][1] ); //4

Additional context

No response

@legocris legocris added the feature request New feature (or a request for one) label Dec 1, 2024
@attic-stuff
Copy link

attic-stuff commented Dec 1, 2024

indexing for structs is not possible as they are, by design, unordered. if you grab a struct off a database on tuesday then whatever is conceptually first in the struct may not be the same if you snatch it again on thursday. if you need order then you should be using an array. a function to grab an array of values would be cool, but determining which struct field each value in that array goes to wouldn't be fun because of the unorderedness of structs. really does sound like you just want an array heh

@legocris
Copy link
Author

legocris commented Dec 2, 2024

indexing for structs is not possible as they are, by design, unordered. if you grab a struct off a database on tuesday then whatever is conceptually first in the struct may not be the same if you snatch it again on thursday. if you need order then you should be using an array. a function to grab an array of values would be cool, but determining which struct field each value in that array goes to wouldn't be fun because of the unorderedness of structs. really does sound like you just want an array heh

Is that really the case? I receive them as a json string and the order is the same as the write order, I mean when using the function struct_get_names. And I'm not asking for that much, just a function that works on whatever order struct_get_names do, better if it's not needed to create a whole array to access to a single value, but if not, array is cool as u say.

@stuckie
Copy link

stuckie commented Dec 2, 2024

Closing as above.
Internally, this is a hash structure, so the indices will not be stable.
An array is likely what you want.

@stuckie stuckie closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2024
@github-project-automation github-project-automation bot moved this from Triage to Done in Team Workload Dec 2, 2024
@attic-stuff
Copy link

Is that really the case?

that is the case, yeppers. like stuckie said, the order is not stable. just because you are seeing a predictave/determined order on your machine does not mean you always would or that your players would at all. its literally happenstance heh

@YYBartT YYBartT moved this from Done to Not Planned in Team Workload Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature (or a request for one)
Projects
Status: Not Planned
Development

No branches or pull requests

3 participants