Page 1 of 2

SortStructuredList()

Posted: Wed Mar 01, 2023 5:44 pm
by Splunk
Hi guys,
I'm probably too stupid again, but I think this is a bug.

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table[3]), TypeOf(Anything\Table)
"Syntax Error" at SortStructuredList()

Why? (PB 5.41)

// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 5:59 pm
by jacdelad
Closing Bracket is missing and you are using OffsetOf wrong:

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table), TypeOf(Anything\Table))
Edit: You want to sort by the third element of Table.l (counting starts with "0", so "3" is the fourth item. if you need the fourth item, then use "+12"):

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table)+8, TypeOf(Anything\Table))
But I've never used the "[]" within structures, so I'm not 100% sure.

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 6:07 pm
by mk-soft
Not supported by 'OffsetOf', but you can calculate it yourself.

@jacdelad,
your calculation is wrong. First element is null. Table index 0 to 4.

Code: Select all

Structure Anything
  Dummy.i
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

Debug OffsetOf(Anything\Table) + 3 * SizeOf(long)

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table) + 3 * SizeOf(long), TypeOf(Anything\Table))

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 6:15 pm
by Kiffi
Splunk wrote: Wed Mar 01, 2023 5:44 pm"Syntax Error" at SortStructuredList()
@Splunk: The bug forum is not for the bugs you make yourself.

Please read before submitting a bug report!

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 6:34 pm
by Splunk
@Kiffi: I am still of the opinion that it is a bug. Because I can't read anywhere in the description that it doesn't work.Did you address the issue, or did you immediately read what "jacdelad" wrote and mistakenly assume I made a mistake?

Thanks to all

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 7:22 pm
by NicTheQuick
Splunk wrote: Wed Mar 01, 2023 6:34 pm @Kiffi: I am still of the opinion that it is a bug. Because I can't read anywhere in the description that it doesn't work.Did you address the issue, or did you immediately read what "jacdelad" wrote and mistakenly assume I made a mistake?

Thanks to all
No, it is not a bug.

Your first error was the missing closing bracket.
Your second error was using OffsetOf wrongly. There is no documentation about the possibility to use the square brackets operator to calculate offsets of static arrays within structure members. But I understand that this would be a nice feature.

The only bug I could imagine is the result of TypeOf(Anything\Table) which is #PB_Long = 5, although in fact it is a static array. But on the other hand it is an static array of type #PB_Long. :wink:
However SizeOf(Anything\Table) returns 20 which is 5 times the size of a Long which is not consistent with TypeOf() then.

I was trying to do something like this as an workaround but because of that inconsistency it does not work:

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

SortStructuredList(void(),
 	#PB_Sort_Ascending,
  	OffsetOf(Anything\Table) + 3 * SizeOf(Anything\Table),
   TypeOf(Anything\Table))

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 7:49 pm
by NicTheQuick
I added a feature request here: viewtopic.php?t=80981

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 7:50 pm
by mk-soft
@NicTheQuick

Now you have made a mistake in calculating the offset!
The 'SizeOf(Anything\Table)' is the total size of the table. So 20 bytes ;)

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 7:57 pm
by Splunk
Thanks NicTheQuick..

Yes, the missing closing bracket was probably lost in the transfer to the forum and is not part of the actual problem.

Since there is (unfortunately) no "clean" solution, I fixed the problem with an auxiliary field.

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 8:01 pm
by mk-soft
Splunk wrote: Wed Mar 01, 2023 7:57 pm Thanks NicTheQuick..

Yes, the missing closing bracket was probably lost in the transfer to the forum and is not part of the actual problem.

Since there is (unfortunately) no "clean" solution, I fixed the problem with an auxiliary field.
Of course it works with self-calculated offset ... My example not tried.

Here again with output.

Code: Select all

Structure Anything
  Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

Debug "set values"
value = 3
ForEach void()
  void()\Table[3] = value
  value - 1
  Debug void()\Table[3]
Next

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table) + 3 * SizeOf(long), #PB_Long)

Debug "sort values"
ForEach void()
  Debug void()\Table[3]
Next

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 8:51 pm
by Splunk
@mk-soft:
Yes, I looked at your suggestion and it does work. But it is not really a "clean" solution.

As a clean solution I tried:

Code: Select all

Bytes = SizeOf(Anything\Table) / TypeOf(Anything\Table)
SortStructuredList(void(), #PB_Sort_Descending, OffsetOf(Anything\Table) +3*Bytes, Bytes)
PB does not accept this. But it is mathematically correct.

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 9:02 pm
by jacdelad
Your "TypeOf"-Parameter for sorting is wrong:

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

Bytes = SizeOf(Anything\Table) / TypeOf(Anything\Table)
SortStructuredList(void(), #PB_Sort_Descending, OffsetOf(Anything\Table) +3*Bytes, TypeOf(Anything\Table))
You gave the size of element, not the type.

Re: SortStructuredList()

Posted: Wed Mar 01, 2023 11:19 pm
by mk-soft
Help, ...
Both wrong

TypeOf for an offset calculation is completely wrong !!! TypeOF does not return a size ...

Offset = offset_of_first_array_element + array_index * sizeof_element
Offset = 0 + 3 * 4
Offset = 12

Re: SortStructuredList()

Posted: Thu Mar 02, 2023 5:41 am
by Splunk
mk-soft wrote: Wed Mar 01, 2023 11:19 pm Help, ...
Both wrong
:mrgreen:

I probably would have reacted the same way if I had read this bullshit. Coincidentally, "TypeOf()" gave the number of elements, which of course is wrong.

The bulletproof solution is:

Code: Select all

Index = 3
LenOfElement = @void()\Table[1] - @void()\Table[0]
SortStructuredList(void(), #PB_Sort_Descending, OffsetOf(Anything\Table) +Index*LenOfElement, TypeOf(Anything\Table))

Re: SortStructuredList()

Posted: Thu Mar 02, 2023 6:23 am
by BarryG
jacdelad wrote: Wed Mar 01, 2023 5:59 pmClosing Bracket is missing
That's why I requested this before -> viewtopic.php?t=76876