Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Simple string manipulation problem
Message
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00540244
Message ID:
00540289
Views:
16
Ok, here is my final program. I haven't compared yours and mine in speed.:
********************************************************************
*  Description.......: InsertAlias - inserts alias before each field in a string
*  Calling Samples...: 
*  Parameter List....: tcStr, tcAlias
*  Created by........: Nadya Nosonovsky 08/06/2001 01:34:01 PM 
*  Modified by.......: 
********************************************************************
lparameter tcStr, tcAlias
** No check for parameters passed
**** First change AND and OR
tcStr=strtran(m.tcStr,".OR."," OR ")
tcStr=strtran(m.tcStr,".AND."," AND ") 
local lcDigits, lcSymbols, lcChars, lnFields, lnI, array laFields[1], lcChar, lcField
lcDigits=[0123456789]
lcChars=[QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm]
lcSymbols=alltrim(chrtran(m.tcStr,m.lcDigits+m.lcChars+['"]+"[]",""))

** Assume, that the table is opened, no check for this
for lnI=1 to len(m.lcSymbols)
    lcChar=substr(m.lcSymbols,m.lnI,1)
    tcStr=strtran(m.tcStr,m.lcChar," "+m.lcChar+" ")
next    
tcStr=" "+m.tcStr+" "
lnFields=afields(laFields)
for lnI=1 to m.lnFields
     lcField=laFields[m.lnI,1]
      tcStr=strtran(m.tcStr," "+m.lcField+" "," "+m.tcAlias+"."+m.lcField+" ")
next
if not "foxtools" $ lower(set('library'))
   set library to foxtools additive
endif
tcStr=reduce(m.tcStr)   
return alltrim(m.tcStr) && this string would contain spaces, but I don't think, it's a problem
>
* Get the list of the fields into array in descending order by the field name lenght
>Select Mytable
>= Afields(laList)
>FOR i=1 TO ALEN(laList,1)
>  Lalist[i,2] = Len(Trim(Lalist[i,2])
>ENDFOR
>ASORT(laList,2, 1)
>
>* Translate all names into unique @nnn format
>FOR i=1 TO ALEN(laList,1)
>  STRTRAN(lcWhere, Trim(laList[i,1]), "@" + PADL(i, 3, "0"))
>ENDFOR
>
>* Translate back adding "aa."
>FOR i=1 TO ALEN(laList,1)
>  STRTRAN(lcWhere, "@" + PADL(i, 3, "0"), "aa." + Trim(laList[i,1]) )
>ENDFOR
>
>
>>>I have an idea
>>>- Put all fields into array laList and sort it in descending order by the field name lenght
>>
>>I had the same idea too, but how would I do N1? :)
>>
>>>- Loop thru array and STRTRAN(lcWhere, Trim(laList(i)), "@" + TRANSFORM(i))
>>
>>???????
>>
>>>- Loop again and STRTRAN(lcWhere, "@" + TRANSFORM(i), "aa." +Trim(laList(i)))
>>>Looks like it should work
>>
>>Anyway, I just have to write this simple code rather than check UT each minute :)
>>
>>>>Hi everyone,
>>>>
>>>>I have some expression, which is either filter() or for() or combination of both. I'd like to insert Alias Lnd1 before each field, which may be in this expression. Example:
>>>>
lcStr=where CODE#PARENTCODE.OR.PCOFLAG="P"
>>>>
>>>>I need to convert it to 'where lnd1.code#lnd1.parentcode OR lnd1.PCOFLAG="P"'
>>>>
>>>>I was thinking about checking this string against each field and do simple strtran, but it would not work, since I have fieldnames, which are part of other field names.
>>>>
>>>>Could you please help me?
>>>>
>>>>Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform