Page 1 of 1
alternate SelectElement()
Posted: Fri Mar 10, 2023 2:57 pm
by Splunk
It would be nice to be able to address list items directly with the list index, e.g.
and not awkwardly via
Code: Select all
SelectElement(List(), INDEX)
Void$ = List()\Period$
I remember that the file handling in the earlier PureBasic versions still had the cumbersome method like "UseFile()". This was then also rationalized away. SelectElement() probably also falls into this category.
Sorry if I bother you with my suggestions. These are just things I don't like and make programming cumbersome.
Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 3:13 pm
by NicTheQuick
+1
That would be a nice addition for syntactic sugar.
But it could also lead to the assumption lists can be handled like arrays while they are both completely different things behind the scenes, in their structure, the memory allocation and speed.
Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 3:16 pm
by STARGÅTE
Splunk wrote: Fri Mar 10, 2023 2:57 pm
It would be nice to be able to address list items directly with the list index
What you describe is an Array !?
Splunk wrote: Fri Mar 10, 2023 2:57 pm
I remember that the file handling in the earlier PureBasic versions still had the cumbersome method like "UseFile()". This was then also rationalized away. SelectElement() probably also falls into this category.
No this is a different story.
In earlier PureBasic versions, functions like WriteLong() and ReadLong() had no file specificator. Here is was a huge mess to read and write parallel in different files.
Linked Lists are specified by there name itself. So you can access different list simultaneously.
Adding the syntax List(INDEX) pretends the thought, you had a direct access to the element, which is not the case!
SelectElement() iterates each time through the list to find the specified element, completely different to an array.
Splunk wrote: Fri Mar 10, 2023 2:57 pm
These are just things I don't like and make programming cumbersome.
An array is probably a more suitable solution.
Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 3:40 pm
by Splunk
@Stargate: I cannot accept your argumentation about internal handling and supposed direct access.
An INDEX argument could be handled internally by PB as SelectElement().
yes, in previous versions of PB WriteLong() etc. had no arguments between the (). And now...?

Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 4:40 pm
by STARGÅTE
Splunk wrote: Fri Mar 10, 2023 3:40 pm
An INDEX argument could be handled internally by PB as SelectElement().
Yes of course. But this is no index in this way, that you have "direct" access to the element.
SelectElement() wrote:Remarks
As lists don't use an index internally, this function will jump compulsory to every element in the list until the target position is reached which will take time if the list is large.
Each time you write List(INDEX) the whole list is iterated until INDEX.
If you have a list of 10000 elements, and you access element 9999, you have 9999 comparisons and iterations.
With your syntax improvement, non-optimal codes are promoted.
I don't know if that would be in interests of the community, but this is only my opinion.
Don't understand it wrong, It is not the syntax improvement I'm against, I would also preserve you to use SelectElement() often, because of the same argumentation. In my opinion there is no need to access a list via an index, because the list can grow and shrink and the index of an element changes.
And if the list is static, then why not using an array and gain time in accessing the elements directly.
Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 5:20 pm
by Splunk
My dear @Stargate:
I am not concerned with speed or anything else.
Void$ = List(INDEX)\Period$
is neither slower nor faster than
SelectElement(List(), INDEX)
Void$ = List()\Period$
but List(INDEX) is a SIMPLIFICATION. Nothing else. It also does not promote non-optimal code.
If you use a List() and not an Array() (or vice versa), you have reasons to do so. And if you use the SelectElement() command, you have reasons for that too, whether fast or slow. I don't think you've never used the SelectElement() function because it's so "dangerous" or slow. You just have to know what you are doing.

Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 5:55 pm
by NicTheQuick
It's called
syntactic sugar. That's all.
Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 6:08 pm
by Splunk
Re: alternate SelectElement()
Posted: Fri Mar 10, 2023 7:24 pm
by STARGÅTE
Splunk wrote: Fri Mar 10, 2023 6:08 pm
Exactly
Ok, I'm fine with this. We both made our point of view clear.
Splunk wrote: Fri Mar 10, 2023 5:20 pm
I don't think you've never used the SelectElement() function because it's so "dangerous" or slow.
Of course I did, I would lie otherwise.