FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Bug in Harbour Descend()
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Bug in Harbour Descend()
Posted: Mon Apr 12, 2021 01:15 PM
This is a sample of the problem:

Code (fw): Select all Collapse
REQUEST HB_CODEPAGE_ITWIN


FUNCTION MAIN()

    LOCAL aData[ 7 ]

    LOCAL i

    HB_SETCODEPAGE( "ITWIN" )

    aData[ 1 ] = { "TEST1", 3107.77 }
    aData[ 2 ] = { "TEST2", 852.07 }
    aData[ 3 ] = { "TEST3", 191.00 }
    aData[ 4 ] = { "TEST4", 148.68 }
    aData[ 5 ] = { "TEST5", 44.73 }
    aData[ 6 ] = { "TEST6", 15.24 }
    aData[ 7 ] = { "TEST7", 255.65 }

    ASORT( aData, , , { | aItem1, aItem2 | Descend( Str( aItem1[ 2 ], 10, 2 ) ) + aItem1[ 1 ] < Descend( Str( aItem2[ 2 ], 10, 2 ) ) + aItem2[ 1 ] } )

    FOR i = 1 TO LEN( aData )
        ? aData[ i, 1 ], aData[ i, 2 ]
    NEXT

    INKEY( 0 )

    RETURN NIL


Result:

Code (fw): Select all Collapse
TEST1       3107.77
TEST2        852.07
TEST5         44.73
TEST6         15.24
TEST7        255.65
TEST4        148.68
TEST3        191.00


Any workaround?

EMG
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Bug in Harbour Descend()
Posted: Mon Apr 12, 2021 02:10 PM
IMHO Test is erronous

Sorting so:

Code (fw): Select all Collapse
ASORT( aData, , , { | aItem1, aItem2 | Descend( Str( aItem1[ 2 ], 10, 2 )  + aItem1[ 1 ] ) < Descend( Str( aItem2[ 2 ], 10, 2 )  + aItem2[ 1 ]  )} )


Regards
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Bug in Harbour Descend()
Posted: Mon Apr 12, 2021 02:57 PM

No, as I want to sort for the second item descending and the first item ascending. Anyway, it doesn't work either.

EMG

Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Bug in Harbour Descend()
Posted: Mon Apr 12, 2021 03:14 PM

The problem seems to be the codepage. Without setting it, the sort order is correct. But the codepage should not affect the behaviour of Descend() function, should it?

EMG

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Bug in Harbour Descend()
Posted: Mon Apr 12, 2021 09:44 PM
I am not commenting on the behaviour of the Descend() function, but I would have approached it in a different way:
Code (fw): Select all Collapse
ASort( aData,nil,nil, { |x,y| If( x[ 2 ] == y[ 2 ], x[ 1 ] > y[ 1 ],  x[ 2 ] > y[ 2 ] ) } )
Regards



G. N. Rao.

Hyderabad, India
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Bug in Harbour Descend()
Posted: Tue Apr 13, 2021 07:33 AM

Thank you. It is not feasible as it would require to change all the ASort() calls. I prefer to remove the codepage.

EMG

Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Bug in Harbour Descend()
Posted: Tue Apr 13, 2021 09:37 AM

It looks like the bug has never been fixed. :-(

EMG

Posts: 375
Joined: Tue Feb 10, 2015 09:48 AM
Re: Bug in Harbour Descend()
Posted: Tue Apr 13, 2021 03:28 PM

have you tryied using StrZero instead of Str?

Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Bug in Harbour Descend()
Posted: Tue Apr 13, 2021 04:42 PM

Yes, the order changes but it's not correct either.

EMG

Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Bug in Harbour Descend()
Posted: Tue Apr 13, 2021 04:59 PM

The purpose of Descend () is to be Clipper compatible. Therefore it is not an error, but an unexpected behavior for those who use codepages
The existence of hb_Descend (), compatible with the codepage system, would be highly desirable.

Posts: 375
Joined: Tue Feb 10, 2015 09:48 AM
Re: Bug in Harbour Descend()
Posted: Tue Apr 13, 2021 08:53 PM
I see that if you remove HB_SETCODEPAGE from your example the array looks correctly sorted...
I investigated a little and I found that there is a flag in the code pages "BinarySort" that indicate if the comparison can be simply binary or more complex, it is true for "en" and false for "itwin"

Try this:
Code (fw): Select all Collapse
    ? chr(199)<chr(205) //--> T
    HB_SETCODEPAGE( "ITWIN" )
    ? chr(199)<chr(205) //--> F

the problem is not the descend... very sad
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Bug in Harbour Descend()
Posted: Mon Oct 17, 2022 11:07 AM

How do you deal with this bug? I need to use codepage with Harbour, otherwise the accented chars are not correctly read from the file system (ie. filenames, Directory(), File(), Memoread(), etc.). But doing so, Descend() function is not working anymore. I can't even build index with Descend() in the key. Any help, please.

Posts: 8523
Joined: Tue Dec 20, 2005 07:36 PM
Re: Bug in Harbour Descend()
Posted: Mon Oct 17, 2022 01:33 PM
Master Enrico, see if it helps:

https://linguagemclipper.com.br/dicas/arrays

Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Bug in Harbour Descend()
Posted: Mon Oct 17, 2022 02:12 PM

Thank you, but I can't found anything about Descend() function in that web page. I need to use Descend() in Harbour just like I use it in xHarbour and used in Clipper. Please look at my first message on this thread for a sample of the problem.