Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Request for a sample to learn from
#1
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.
#2
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
Code:
Copy      Help
/
function str&inputString
ARRAY(str) sortArray=inpString
,sortArray.sort(2)
,out sortedArray
inpString=sortArray
out inpString
#3
Is this what you're looking for?

Member function str.CapWords
Code:
Copy      Help
;/

;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])
#4
this also supports non-ASCII first character
Member function str.listSort
Code:
Copy      Help
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
#5
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.
#6
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"
#7
combining two functions...

Member function str.CapWords2
Code:
Copy      Help
;/

;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
Code:
Copy      Help
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
#8
Thank you for your help once again Lucas.
I will be studying your functions tomorrow.


Forum Jump:


Users browsing this thread: 2 Guest(s)