- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How do I use sortq? I can't get it to work and I don't understand the documentation in dfport or in help. Here's what I have so far:
subroutine loadcuts (my subroutine) use dfport .... integer(2), external :: cmp_char CHARACTER cv5*5(10) ...... !sort width strings CALL QSORT (cv5,10,5,cmp_char) integer(2) function cmp_char(a1, a2) character*5 a1, a2 if (a1.lt.a2) cmp_char=1 if (a1.eq.a2) cmp_char=0 if (a1.gt.a2) cmp_char=-1 end function
Link Copied
11 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It looks OK at first sight, except that I'm not sure whether .lt. and .gt. work with strings (my 5.0 docs do not mention that -- maybe an extension was added in the meantime) -- I'd expect a compile-time error for that.
Take a look on intrinsics LGT and LLT instead.
Jugoslav
Take a look on intrinsics LGT and LLT instead.
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
.lt., etc., are fine with strings. That's standard. LLT and LGT are for where you want to always use the ASCII collating sequence, even on machines that don't use ASCII (such as IBM System390). In CVF, they're equivalent.
I haven't looked at the manual yet to see if I can tell what's going wrong (writing this from home.)
Steve
I haven't looked at the manual yet to see if I can tell what's going wrong (writing this from home.)
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I hope you can find it, Steve. I searched and viewed every QSORT in the Samples directory and saw no example for characters.
I also searched on SORTQ in help and read every word.....probably something I'm missing, of course...but your advice would save me in this case.
Keith Richardson
I also searched on SORTQ in help and read every word.....probably something I'm missing, of course...but your advice would save me in this case.
Keith Richardson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your comparison function is backwards - it should return 1 if a1 is greater than a2, not the other way around. Other than that, I don't see anything wrong with this.
If correcting the comparison function doesn't fix the problem, please send a complete example to us at vf-support@compaq.com and we'll take a look. I tried the following test case and it seemed to work fine:
Steve
If correcting the comparison function doesn't fix the problem, please send a complete example to us at vf-support@compaq.com and we'll take a look. I tried the following test case and it seemed to work fine:
program sort use dfport character*5 :: carray(5) = & (/'BBBBB','CCCCC','AAAAA','EEEEE','DDDDD'/) integer(2), external :: compare call qsort (carray, 5, 5, compare) write (*,*) carray end program sort integer(2) function compare (arg1,arg2) character*(*) arg1, arg2 if (arg1 > arg2) then compare = 1 else if (arg1 == arg2) then compare = 0 else compare = -1 end if return end function compare
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Makes me think... since QSORT is basically a stupid thing (written in C?)
which knows nothing about prototype of cmp_char (it knows only its address), how does it handle hidden string length arguments? !DEC$ATTRIBUTES REFERENCE on a1 and a2 IMO should help.
If I'm right, Steve, how does your example work? I'd expect that you get
that len(arg1)=loc(arg2), while arg2 contains garbage. What's actually the difference between declaring character*5 and character*(*) arg1, arg2?
Jugoslav
which knows nothing about prototype of cmp_char (it knows only its address), how does it handle hidden string length arguments? !DEC$ATTRIBUTES REFERENCE on a1 and a2 IMO should help.
If I'm right, Steve, how does your example work? I'd expect that you get
that len(arg1)=loc(arg2), while arg2 contains garbage. What's actually the difference between declaring character*5 and character*(*) arg1, arg2?
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Jugoslav,
Read the declaration of QSORT in DFPORT.F90 and your questions will be answered. Briefly, it's a generic, with a specific routine for CHARACTER arguments. You can even extend the generic for your own types. Rather clever, I think.
Steve
Read the declaration of QSORT in DFPORT.F90 and your questions will be answered. Briefly, it's a generic, with a specific routine for CHARACTER arguments. You can even extend the generic for your own types. Rather clever, I think.
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
...and, Keith, Steve, what's the setting in your Project/Settings/Fortran/External Procedures/String length arg passing?
IMO for your examples to work, it should be "After all args", which is not the default?
Jugoslav
IMO for your examples to work, it should be "After all args", which is not the default?
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Uh, Steve, you were so fast in replying that I didn't make it to read your
answer before I posted another. OK, you're right, I retreat and I state the opposite. Still, could the setting I mentioned possibly affect the mechanism (though with the default there apparently should be no problem)?
Jugoslav
answer before I posted another. OK, you're right, I retreat and I state the opposite. Still, could the setting I mentioned possibly affect the mechanism (though with the default there apparently should be no problem)?
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is why we created the DEFAULT attribute in 6.1 and used it in all our modules.
Steve
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, gentlemen, once again. My qsort works great now.
I just had to add an Attribute statement - since I'm calling the integer function from within a callback, and I need the following statement for all my callbacks (since the project is mixed C++ and DVF):
!DEC$ ATTRIBUTES DEFAULT :: [subroutine name]
Keith Richardson
I just had to add an Attribute statement - since I'm calling the integer function from within a callback, and I need the following statement for all my callbacks (since the project is mixed C++ and DVF):
!DEC$ ATTRIBUTES DEFAULT :: [subroutine name]
Keith Richardson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What I mean to say is, I had to add the attributes statement to the integer function, since I called it from within a callback that has that attributes statement. I guess they couldn't talk to one another otherwise.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page