stam wrote: Sun Mar 07, 2021 10:59 pm
SparkOut wrote: Sun Mar 07, 2021 10:51 pm
I think you have gathered, what I think Richard was trying to draw attention to
No i get that - just not sure why it's being raised as a discussion point.
If there were true arrays in LC we wouldn't need this rigamarole
One person's "ragamarole" is another person's "learning". This thread has taught me much. I can't recall a day in the 30 years I've been working with computers where I didn't learn something new. Along the way I try to pass along some of what I've learned, some days more successfully than others.
There are many types of data structures in computing, arrays among them. Arrays are most commonly found in two forms: associative and indexed. Where data structures differ doesn't mean some of them are wrong, it just means they're different. The key to using any data structure well is to understand its nature.
You likely know the details provided in this reply, but future readers who will come across this thread when learning about arrays may not. And while we don't really need to know the deep underpinnings, a passing familiarity helps us understand some important essentials for working with arrays. Feel free to skip this post if it seems wordy or uninteresting. I'm writing mostly for future readers who, like so many others over the years, will have questions about how to work with arrays in LiveCode powerfully.
Associative arrays are an immensely flexible and efficient tool for associating a label with a value. The label can be any string from 1 to 255 characters long, and the value can be just about any data, whether text, binary, or even another array. The range of uses for this structure are vast, which is why so many languages support associative arrays (going forward I'll refer to them as simply "arrays", since they're the type LC Script currently offers).
The thing that makes them so efficient for such a broad range of tasks is the very thing that often leads to confusion where their nature is not understood. They're a collection of virtual buckets, in which each contains pointers to disparate memory locations. A label is assigned to a given bucket by a hash algorithm, allowing a fairly small number of name-address lookups to be stored in a way that affords rapid access. The
Wikipedia page I linked to early in this discussion covers this in more satisfying depth.
The biggest takeway is that an array isn't a contiguous bytestream, which explains why
an array can't be sorted, nor even displayed, transported or stored.
That awareness leads us to understanding how the posts in this thread come together:
If we ask "How can an array be sorted?", with the awareness of an array's essential nature we might also wonder "How can an array even be displayed?"
And it turns out answer for both is the same: the code used within the UI display mechanism we've chosen.
LC provides two UI options for displaying data from an array: the Tree widget and the DataGrid.
Both walk through the elements of an array, extracting strings as they go, rendering them on screen. And both include options for sorting
the rendered strings.
Neither displays an array directly, because it can't.
So when we see UI element displaying array contents, it's useful to keep in mind that what we're seeing are strings copied from parts the array, and not the array itself. And with that in mind we can appreciate that any sorting we see in a UI control is a feature of that UI control, operating only on its string copies.
This awareness helps our work in many ways:
- It helps us understand how to use the control.
- It invites us to
explore the options provided by the control for sorting its string copies.
- It empowers us to craft a nearly infinite variety of custom controls for displaying array contents in any way we might need or want.
- It makes is better equipped to work with arrays in
contexts outside of merely displaying them to the user.
And perhaps equally important:
- It helps us anticipate solutions where simple delimited strings may be a better fit.
No single data structure is best in all cases. Knowing the nature of each lets us choose with confidence.
This thread is a good example:
The post that started this was from someone who obtained data from an SQL query and wanted to display it in a sorted list. The only issue he had looks like a simple syntax error, likely easily fixed.
But now, with all we've covered in this thread, we might also ask another question: why is this being done in an array?
To do what the OP requested requires:
1. Call revDBQuery
2. Write a handler to convert the resulting delimited text to an array
3. Find a handler in the forums to prepend that array with an outer array of sequential integers according to a sort order
4. Use the resulting compounded array to populate the DataGrid using its array option
Or we could:
1. Call revDBQuery
2. Put the result in a field
If we need a DataGrid we can replaced #2 there with "populate the DataGrid using its text option".
This is why so many of my replies in this forum
ask what the goal is. Until the OP returns to answer that, the range of possibilities afforded by LC's rich language are too vast to know the best answer for the task at hand.
As we learn our tools ever better, we can pick the best tool for the job we're facing.
Arrays are great.
Chunk expressions are great.
LiveCode is great.
Enjoy it all.