home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
mbug
/
mbug001.arc
/
PART8.IQS
/
PART8.INS
Wrap
Text File
|
1979-12-31
|
9KB
|
247 lines
.po0
dBAS┼ I╔ PAR╘ 8
MACROS
DBAS┼ II'≤ macr∩ facilit∙ i≤ onσ oµ thσ mos⌠ powerfu∞ facilitie≤ oµ ì
thσ language« It'≤ s∩ powerfu∞ it'≤ dangerous¼ anΣ iµ i⌠ wasn'⌠ fo≥ thσ fac⌠ ì
tha⌠ somσ thing≤ woulΣ bσ impossiblσ withou⌠ i⌠ ¿ anΣ eveε morσ thing≤ s∩ mucΦ ì
easie≥ )¼ ╔ woulΣ wan⌠ t∩ seσ i⌠ takeε out.
Beforσ ╔ explaiε why¼ we'Σ bette≥ examinσ ho≈ i⌠ works« looδ a⌠ thi≤ ì
shor⌠ commanΣ filσ :
ERAS┼
└ 12,3░ SA┘ 'Filσ t∩ useº ACCEPT
T╧ filename
US┼ &filename
RETURN
It'≤ fairl∙ obviou≤ what'≤ happeninτ here« Thσ prograφ ask≤ fo≥ ß ì
filenamσ anΣ store≤ i⌠ iε ß memor∙ variablσ calleΣ 'filename'« Thσ nex⌠ linσ ì
i≤ ß standarΣ US┼ command¼ excep⌠ tha⌠ thσ variablσ filenamσ crop≤ up¼ ì
precedeΣ b∙ aε ampersanΣ (&).
Wheneve≥ dBasσ come≤ acros≤ aε &¼ i⌠ replace≤ thσ variablσ namσ tha⌠ ì
follow≤ witΦ thσ valuσ oµ thσ variable« Thi≤ i≤ purel∙ ß textua∞ substitution╗ ì
thereforσ thσ variablσ mus⌠ bσ oµ strinτ type¼ anΣ thσ resul⌠ wil∞ bσ ì
evaluateΣ purel∙ a≤ ß string.
Thi≤ caε leaΣ t∩ somσ confusion« Fo≥ examplσ :
STOR┼ '3º T╧ var1
STOR┼ '4º T╧ var2
┐ &var▒ ½ ª var2
7
┐ ª var1&var2
34
?'&var1º ½ '&var2º 34
A≤ ß result¼ codσ tha⌠ make≤ extensivσ usσ oµ macro≤ caε bσ ver∙ ì
confusinτ indeed« However¼ i⌠ i≤ possiblσ t∩ d∩ thing≤ tha⌠ woulΣ otherwisσ bσ ì
impossible¼ sucΦ a≤ writinτ self-modifyinτ code¼ o≥ abbreviateΣ men⌡ routine≤ ì
║ WAI╘ fo≥ ß men⌡ selectioε anΣ theε D╧ &choice« Herσ i≤ aε examplσ oµ ß ì
simplσ men⌡ prograφ usinτ ß macr∩ :
MACMENU.CM─ b∙ ╠ ╟ BELL
Illustrate≤ usσ oµ macro≤ fo≥ men⌡ operation
ERASE
D╧ WHIL┼ T
└ 5,1▓ SA┘ 'AdΣ Names'
└ 6,1▓ SA┘ 'Deletσ Names'
└ 7,1▓ SA┘ 'Quit'
└ 21,1▓ SA┘ 'Choice'
SE╘ CONSOL┼ OFF
WAI╘ T╧ Choice cont....è SE╘ CONSOL┼ ON
STOR┼ !(choice⌐ T╧ choice
I╞ @(choice,'ADQ'⌐ ╜ 0
┐ chr(7)
ELSE
D╧ SUB&choice
ENDIF
ENDDO
RETURN
MOR┼ USE╙ FO╥ MACROS.
Othe≥ use≤ fo≥ macro≤ includσ cramminτ a≤ mucΦ a≤ possiblσ int∩ ì
dBasσ line≤ b∙ replacinτ comple° function≤ witΦ macro≤ :
STOR┼ 'STOR┼ $(disdate,7,2)+$(disdate,4,2)+$(disdate,1,2⌐ T╧ mdate'
T╧ makedate
.
.
othe≥ code
.
.
&makedate
However¼ thσ primar∙ applicatioε fo≥ thσ macr∩ functioε i≤ thσ US┼ ì
anΣ FIN─ statements« Fo≥ example¼ thσ codσ segmen⌠ :
STOR┼ 'Smithº T╧ name
FIN─ name
wil∞ no⌠ work¼ sincσ dBasσ wil∞ literall∙ searcΦ fo≥ 'Mr« name'« Instead¼ usσ ì
thi≤ techniquσ :
STOR┼ 'Smithº T╧ name
FIN─ &name
┴ simila≥ techniquσ i≤ useΣ witΦ thσ US┼ statement.
GUIDELINES
Macro≤ arσ invaluable╗ a≤ wσ havσ seen¼ thσ FIN─ anΣ US┼ command≤ ì
caε hardl∙ bσ useΣ insidσ program≤ withou⌠ them« A⌠ thσ samσ time¼ the∙ makσ ì
debugginτ mucΦ morσ difficult¼ sincσ the∙ literall∙ changσ thσ tex⌠ oµ thσ ì
prograφ scorcσ codσ itself« Fo≥ tha⌠ reasoε alone¼ ╔ aφ no⌠ suggestinτ somσ oµ ì
thσ morσ arcanσ use≤ oµ macros« Iµ yo⌡ arσ sufficientl∙ perverse¼ yo⌡ wil∞ n∩ ì
doub⌠ bσ ablσ t∩ comσ u≡ witΦ delightfu∞ minΣ boggler≤ oµ you≥ own« Iε thσ ì
interes⌠ oµ retaininτ you≥ sanity¼ therσ arσ ß fe≈ guideline≤ yo⌡ shoulΣ bea≥ ì
iε minΣ wheε usinτ macros.
Firs⌠ oµ all¼ conside≥ thσ operatioε oµ dbasσ itself« Executioε oµ ì
prograφ statement≤ i≤ ß tw∩ o≥ morσ ste≡ process« Firs⌠ oµ all¼ thσ macr∩ pre-ì
processo≥ scan≤ acros≤ thσ linσ lookinτ fo≥ ampersands« Iµ i⌠ find≤ one¼ i⌠ ì
theε extract≤ thσ followinτ variablσ anΣ replace≤ thσ ampersanΣ anΣ variablσ ì
namσ witΦ thσ content≤ oµ thσ variable« Iµ i⌠ find≤ ß seconΣ ampersanΣ beforσ ì
thσ variablσ name¼ i⌠ wil∞ evaluatσ tha⌠ macr∩ first¼ anΣ theε returε t∩ ì
perforφ ß seconΣ pass« Fo≥ thσ technicall∙ minded¼ thσ macr∩ pre-processo≥ ìèappear≤ t∩ bσ recursive« Iε othe≥ words¼ i⌠ keep≤ re-scanninτ thσ linσ unti∞ ì
i⌠ ha≤ removeΣ thσ ampersands« Finally¼ afte≥ al∞ ampersand≤ havσ beeε ì
removed¼ thσ resultinτ linσ i≤ actuall∙ executeΣ b∙ dBasσ interpreter.
RUL┼ ON┼ ║ thσ resultinτ tex⌠ linσ mus⌠ stil∞ bσ standarΣ ASCI╔ text¼ ì
thereforσ AN┘ VARIABLE╙ USE─ FO╥ MACR╧ SUBSTITUTIO╬ MUS╘ B┼ O╞ TYP┼ CHARACTER« ì
Iµ yo⌡ wan⌠ t∩ inser⌠ ß number¼ presentl∙ storeΣ iε ß variable¼ int∩ ß commanΣ ì
line¼ yo⌡ mus⌠ firs⌠ conver⌠ i⌠ int∩ ß characte≥ variablσ usinτ thσ ST╥ ì
function« Failurσ t∩ d∩ this¼ o≥ an∙ attempt≤ t∩ usσ numeriπ variable≤ iε ì
macr∩ substitution¼ wil∞ crasΦ earlie≥ version≤ oµ dBasσ o≥ resul⌠ iε aε erro≥ ì
messagσ iε V2.4.
RUL┼ TW╧ ║ TH┼ RESULTIN╟ TEX╘ LIN┼ MUS╘ STIL╠ OBE┘ TH┼ RULE╙ O╞ TH┼ ì
LANGUAGE« Fo≥ example¼ yo⌡ canno⌠ usσ macr∩ substitutioε t∩ producσ variablσ ì
name≤ longe≥ thaε teε characters« Iµ iε doubt¼ tes⌠ i⌠ oε pape≥ first║ d∩ thσ ì
macr∩ substitutioε anΣ seσ iµ thσ resultinτ statemen⌠ i≤ lega∞ dBasσ code.
RUL┼ THRE┼ ║ YO╒ CANNO╘ US┼ DATABAS┼ FIELD╙ FO╥ MACR╧ SUBSTITUTION« The∙ ì
arσ differen⌠ animal≤ froφ memor∙ variables¼ remember.
RUL┼ FOU╥ ║ MACR╧ SUBSTITUTIO╬ TAKE╙ PLAC┼ ANYWHER┼ I╬ ┴ dBAS┼ PROGRAM¼ ì
includinτ insidσ quoteΣ tex⌠ o≥ strinτ constants« E.╟ :
STOR┼ 'Surnameº T╧ indname
┐ 'Thσ curren⌠ inde° i≤ &indname'
wil∞ work« Thi≤ i≤ sometime≤ ß usefu∞ side-effect¼ bu⌠ usuall∙ ì
therσ i≤ ß morσ conventiona∞ anΣ understandablσ wa∙ t∩ ge⌠ thσ samσ result« ì
Fo≥ examplσ ║
STOR┼ 'surnameº T╧ indname
┐ 'Thσ curren⌠ inde° i≤ º ½ indname
wil∞ als∩ work¼ anΣ i≤ morσ understandablσ t∩ programmer≤ ì
familia≥ witΦ BASI├ bu⌠ no⌠ dBase« It'≤ probabl∙ faste≥ a≤ well.
RUL┼ FIV┼ ║ MACR╧ SUBSTITUTIO╬ TAKE╙ PLAC┼ U╨ T╧ ANOTHE╥ AMPERSAND¼ ┴ ì
SPACE¼ ┴ LIN┼ END¼ O╥ A╬ OPERATOR« Thi≤ caε bσ useΣ t∩ forφ filename≤ ou⌠ oµ ì
tw∩ concentrateΣ parts« Thesσ simplσ guideline≤ wil∞ hel≡ t∩ kee≡ yo⌡ straigh⌠ ì
iε usinτ macros« Anothe≥ probleφ t∩ watcΦ ou⌠ fo≥ i≤ thσ mysteriou≤ erro≥ o≥ ì
correctioε message« Fo≥ examplσ iε you≥ code¼ yo⌡ migh⌠ havσ somethinτ likσ :
STOR┼ '1º T╧ fred
STOR┼ 'fredº T╧ varname
.
.
STOR┼ &varnamσ ½ ▒ T╧ &varname
è Wheε thi≤ runs¼ you'l∞ ge⌠ aε erro≥ message¼ sincσ variablσ ì
'frtdº contain≤ '1'¼ anΣ yo⌡ can'⌠ incremen⌠ ß string« However¼ thσ 'correc⌠ ì
anΣ retry?º strinτ wil∞ bσ
STOR┼ freΣ ½ ▒ T╧ fred
Don'⌠ g∩ lookinτ througΦ you≥ codσ fo≥ tha⌠ line¼ a≤ i⌠ doesn'⌠ ì
exist╗ it'≤ thσ resul⌠ oµ thσ macr∩ substitutioε tha⌠ thσ interprete≥ i≤ ì
tellinτ yo⌡ i≤ wrong« Thi≤ kinΣ oµ thinτ caε makσ debugginτ you≥ prograφ ß ì
nightmare.
DBAS┼ FUNCTION╙ :
DBasσ ha≤ ß selectioε oµ function≤ whicΦ returε value≤ afte≥ performinτ ì
somσ operation«
ú - recorΣ numbe≥ functioε - thi≤ return≤ thσ numbe≥ oµ thσ curren⌠ recorΣ a⌠ ì
whicΦ thσ databasσ i≤ positioned« Afte≥ aε unsuccessfu∞ finΣ thi≤ i≤ zer∩ ¿ ì
witΦ SE╘ TAL╦ OFF¼ thi≤ i≤ thσ onl∙ wa∙ you'l∞ kno≈ tha⌠ n∩ matcΦ wa≤ found)«
- deleteΣ recorΣ functioε - return≤ truσ o≥ false¼ dependinτ oε weathe≥ thσ ì
recorΣ ha≤ beeε deleteΣ o≥ not« Remembe≥ tha⌠ eacΦ recorΣ iε ß .DB╞ ha≤ ß ì
singlσ bytσ a⌠ thσ beginninτ whicΦ contain≤ aε asterisδ iµ tha⌠ recorΣ ha≤ ì
beeε deleted.
EO╞ - enΣ oµ filσ - return≤ truσ iµ yo⌡ tr∙ t∩ reaΣ beyonΣ thσ enΣ oµ ß file« ì
UseΣ t∩ contro∞ D╧ WIL┼ loops.
!(⌐ - uppe≥ casσ conversioε functioε - convert≤ thσ variablσ o≥ strinτ ì
litera∞ iε thσ bracket≤ t∩ uppe≥ case« Thu≤ !('hell∩ world!'⌐ i≤ HELL╧ WORLD!.
TYP┼ (⌐ - return≤ thσ typσ oµ variablσ o≥ expressioε iε thσ parentheses« ì
Eithe≥ C¼ N¼ ╠ o≥ ╒ wil∞ bσ returned¼ indicatinτ ß character¼ numeric¼ logica∞ ì
o≥ undefined.
IN╘ (⌐ - strinτ o≥ intege≥ conversioε - noticσ tha⌠ i⌠ onl∙ return≤ aε ì
intege≥ value¼ witΦ n∩ fractiona∞ part«
STR(n,l,p⌐ - numeriπ t∩ strinτ conversioε - convert≤ numeriπ variablσ ε t∩ ß ì
strinτ oµ lengtΦ l¼ witΦ (optionally⌐ ≡ place≤ afte≥ thσ decima∞ point.
LE╬ (⌐ - strinτ length
$(s,p,l⌐ - substrinτ extractioε - extract≤ ∞ character≤ froφ strinτ ≤ ì
startinτ a⌠ positioε p« eg║ $('ABCDEFG',4,2⌐ i≤ "DE".
@(s1,s2⌐ - strinτ searcΦ - return≤ thσ positioε oµ thσ strinτ s▒ iε strinτ ì
s2« Thu≤ @('CD','ABCDE'⌐ i≤ 3.
CH╥ (⌐ - ASCI╔ character« Thi≤ work≤ jus⌠ likσ CHRñ iε BASIC« Thu≤ CHR(65⌐ i≤ ìè"A"¼ whilσ CHR(27⌐ i≤ thσ escapσ character.
RAN╦ (⌐ - decima∞ valuσ oµ character« Thi≤ i≤ thσ reversσ oµ CHR¼ sincσ i⌠ ì
return≤ thσ positioε oµ thσ characte≥ iε thσ ASCI╔ sequence« Thu≤ RANK('C'⌐ i≤ ì
67.
FIL┼ (⌐ - doe≤ filσ exist┐ WARNING!!í D╧ NO╘ US┼ whilσ an∙ databasσ ì
(.DBF⌐ filσ i≤ open¼ a≤ dBasσ wil∞ erasσ ß numbe≥ oµ file≤ froφ you≥ disk╗ ì
usuall∙ thσ databasσ filσ anΣ an∙ associateΣ indexes¼ bu⌠ ofteε ß numbe≥ oµ ì
other≤ fo≥ gooΣ measure« Iµ yo⌡ stil∞ fee∞ safσ usinτ thi≤ function¼ i⌠ ì
return≤ .T« o≥ .F.╗ fo≥ examplσ :
I╞ FILE('BKINV.DBF')
US┼ BKINV
ENDIF
TRI═ (⌐ - eliminate≤ trailinτ blank≤ - becausσ dBasσ field≤ arσ oµ fixeΣ ì
length¼ the∙ wil∞ ofteε havσ extrß paddinτ space≤ oε thσ enΣ oµ thσ contents«
TRI═ get≤ riΣ oµ thesσ blanks╗ fo≥ examplσ :
┐ TRIM(town))+º '½ TRIM(state)+º º postcode
NEX╘ PAR╘ INCLUDE╙ @...SAY...GE╘ statement≤ anΣ forma⌠ files.
EN─ O╞ PAR╘ 8