Just starting out? Need help? Post your questions and find answers here.
Armoured
Enthusiast
Posts: 348 Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:
Post
by Armoured » Tue Sep 20, 2022 1:08 pm
Hi
Code: Select all
Dim numbers.a(4)
numbers(0) = 4
numbers(1) = 3
numbers(2) = 1
numbers(3) = 2
SortArray(numbers(),#PB_Sort_Ascending)
For i = 0 To 3
Debug Str(numbers(i))
Next i
I was expected an output like:
1
2
3
4
but the result is:
0
1
2
3
I used PureBasic 6.0 LTS
RASHAD
PureBasic Expert
Posts: 4664 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » Tue Sep 20, 2022 1:14 pm
No bug
Code: Select all
Dim numbers.a(4)
numbers(0) = 4
numbers(1) = 3
numbers(2) = 1
numbers(3) = 2
SortArray(numbers(),#PB_Sort_Ascending,0,3)
For i = 0 To 3
Debug Str(numbers(i))
Next i
Egypt my love
Armoured
Enthusiast
Posts: 348 Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:
Post
by Armoured » Tue Sep 20, 2022 1:22 pm
Hi Reshad,
Seems that an extra value is allways inserted in the array with value 0 after the sort.
look this:
Code: Select all
Dim numbers.a(4)
numbers(0) = 4
numbers(1) = 3
numbers(2) = 1
numbers(3) = 2
SortArray(numbers(),#PB_Sort_Ascending,0,3)
For i = 0 To 4
Debug Str(numbers(i))
Next i
I was expected an error but the output is:
1
2
3
4
0
RASHAD
PureBasic Expert
Posts: 4664 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » Tue Sep 20, 2022 1:29 pm
Why
For i = 0 To 4
If you only need from 0 to 3 = 4 items
Or
Use
Dim numbers.a(3)
Still NO BUG
Egypt my love
Armoured
Enthusiast
Posts: 348 Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:
Post
by Armoured » Tue Sep 20, 2022 1:48 pm
From the PureBasic manual on SortArray() parameters: "Start, End (optional) The index of the first and last element in the array that should be sorted.
If these parameters are not specified, then the whole array is sorted. "
As I understand it the whole array is the one without the extra value set to zero and for this reason this is a bug
RASHAD
PureBasic Expert
Posts: 4664 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » Tue Sep 20, 2022 1:56 pm
PB DIM always + one extra item
But if you are going to argue and you belief it's a bug GO and post a Bug report
Egypt my love
Armoured
Enthusiast
Posts: 348 Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:
Post
by Armoured » Tue Sep 20, 2022 2:14 pm
RASHAD wrote: ↑ Tue Sep 20, 2022 1:56 pm
PB DIM always + one extra item
The problem is not the Dim but the extra element that is also ordered and inserted in the resulting sorted array
RASHAD wrote: ↑ Tue Sep 20, 2022 1:56 pm
But if you are going to argue and you belief it's a bug GO and post a Bug report
In the "Bugs" section of the forum I read that first I have to post any bugs found here and only after eventually in the right section
NicTheQuick
Addict
Posts: 1227 Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:
Post
by NicTheQuick » Tue Sep 20, 2022 2:20 pm
When you write
it will create an array with 5 elements, not 4.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
jacdelad
Addict
Posts: 1488 Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:
Post
by jacdelad » Tue Sep 20, 2022 2:29 pm
...plus there's value assigned to element 4, so it's 0 from the beginning.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Armoured
Enthusiast
Posts: 348 Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:
Post
by Armoured » Tue Sep 20, 2022 4:08 pm
Ok thanks to all!
netmaestro
PureBasic Bullfrog
Posts: 8433 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Tue Sep 20, 2022 7:15 pm
Simple, numbers(4) has nothing in it and you're sorting the whole array so the sort relocates it to the bottom of the array.
SortArray(numbers(),#PB_Sort_Ascending, 0, 3) matches the way you filled the array and will give you the result you want.
BERESHEIT