Posts: 22
Threads: 4
Joined: Apr 2013
Can anyone provide a sample of a function for me to study. The function would take a string variable passed from a macro, sort it alphabetically and capitalise the first letter of each word then format the rest of the word lowercase. Then update the varable value in the macro.
The string passed from the macro would most likely be obtained using getfile and loading an existing text file. Any text file loaded will be a simple list, one item per line. There could be any number of items in this file, unsorted and of varying case upper/lower or mixed.
I am learning the workings of QM steadily, but not having a programming background I need to study and breakdown samples and other users macros to fully understand everything.
Posts: 22
Threads: 4
Joined: Apr 2013
OK, I have managed to design a member function which performs an alphabetical sort and returns the string to the macro. This seems to work fine.
I need to read up on Functions/member Functions to find which type is more suitable.
Anyway I still need to come up with a method of altering the case of the words in the list.
Member function
str.listSort
/
function str&inputString
ARRAY(str) sortArray=inpString
,sortArray.sort(2)
,out sortedArray
inpString=sortArray
out inpString
Posts: 858
Threads: 196
Joined: Apr 2005
Is this what you're looking for?
Member function
str.CapWords
;/
;Capitalizes first character of each word.
;EXAMPLE
;str s="quick macros"
;s.CapWords
;out s ;;"Quick Macros"
ARRAY(lpstr) a
tok this a
int i
for i 0 a.len
,a[i][0]=toupper(a[i][0])
Posts: 12,087
Threads: 142
Joined: Dec 2002
this also supports non-ASCII first character
Member function
str.listSort
function [sort] ;;sort: 1 descending, 2 case insens., 4 ling, 6 ling/insens, 8 numbers/ling/insens, 0x100 date.
;EXAMPLE
;str s=
;;oNe
;;five
;;ą non ASCII
;s.listSort(6)
;out s
ARRAY(str) a=this
int i
for i 0 a.len
,str& s=a[i]
,if(!s.len) continue
,s.lcase
,if s[0]<128
,,s[0]=toupper(s[0])
,else
,,BSTR b=s
,,b[0]=CharUpperW(+b[0])
,,s=b
a.sort(sort)
this=a
Posts: 22
Threads: 4
Joined: Apr 2013
Thank you so much, Gintaras and Lucus.
Hopefully I will be able to break these two functions down and learn how they work. I would never have achieved this without your help, I still have a vast amount to learn.
One more question if I may, using the same text file setup, how would I write a function to have the list passed to it by a variable from a macro along with a new variable picked from say a dialog box or other user input.
The function would then check the list to see if this new string already exists. If it does not it would be appended to the list and update the variable in the macro and update the text file.
Once again I am very grateful for your help, I really do want to learn and master QM.
Posts: 22
Threads: 4
Joined: Apr 2013
Hello Gintaras and Lucas.
Sorry to trouble you both again. I am working through both of your functions and was wondering what modifications would be needed if any or all lines in the text file contained more
than one word and I wanted to capitalise the first letter of every word e.g " How Can I Help You"
Posts: 858
Threads: 196
Joined: Apr 2005
combining two functions...
Member function
str.CapWords2
;/
;Capitalizes first character of each word.
;EXAMPLE
;str s="quick macros"
;s.CapWords
;out s ;;"Quick Macros"
ARRAY(lpstr) a
this.lcase
tok this a
int i
for i 0 a.len
,if(a[i][0]<128)
,,a[i][0]=toupper(a[i][0])
,else
,,BSTR b=a[i]
,,b[0]=CharUpperW(+b[0])
,,a[i]=b
Macro
Macro23
out
str text="this is a test of three lines.[]how can I help you.[]line three"
out text
out "----"
text.CapWords2
out text
out "----"
text.Sort(6)
out text
Posts: 22
Threads: 4
Joined: Apr 2013
Thank you for your help once again Lucas.
I will be studying your functions tomorrow.