Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2004
    Posts
    19

    Unanswered: foxpro command question

    Ok, I am not sure why I am having trouble with this but maybe someone out thier can help.
    I am using foxpro 2.6 and have a characeter field in my database, 66 in length that has X 's marked in different positions, example "X XXX X"
    I need to convert that to the actual numeric position into another field followed but a comma, example from above "1,3,4,5,7"
    I have tried the at() command and no luck. If anyone can come up with a way to convert that I would greatly appreciate it.
    Thanks in advance!

  2. #2
    Join Date
    Sep 2003
    Posts
    17
    I think you have to write small function that gets the string as parameter and iterate for each character based on length of the string in FOR loop. if current position of the character contains the 'X' then add the current counter in the string.

    Hope this helps,
    let me know if you want working example.

    Thanks,
    James

  3. #3
    Join Date
    Apr 2004
    Posts
    19

    Working Example

    James, thanks for the reply. I did mess around with a small program but could not get anything to do what it needed. I started with setting a variable to the len(allt(fieldname)) and then doing a for next loop with the variable being after the for condition, then trying to use the at() clause to detirmine if their was an X in the position and replac my string with the numeric position followed but a ',' but I don't think at() is what I want to use.
    Can you provide a working example?

    Thanks!

    Rob

  4. #4
    Join Date
    Sep 2003
    Posts
    17
    Hi Rob,

    Please find following code in VB that does the Job:

    Dim ln, I As Integer
    Dim s1 As String, chr As String
    Dim numStr As String
    s1 = "X XXX X"
    ln = Len(s1)
    numStr = ""
    For I = 1 To ln Step 1
    chr = Mid(s1, I, 1)
    If chr = "X" Then
    numStr = numStr & Trim(str(I)) & ","
    End If
    Next

    MsgBox Mid(numStr, 1, Len(numStr) - 1)

    Since I don't have foxpro installed I have provided code in VB however, I have written code for foxpro but it is not validated. Please find below Foxpro code. You can write similar foxpro function to do the job.

    ln = 0
    I = 0
    chr = ""
    numStr = ""
    s1 = "X XXX X"

    ln = Len(s1)
    numStr = ""
    For I = 1 To ln Step 1
    chr = substring (s1, I, 1)
    If chr = "X" Then
    numStr = numStr & Trim(str(I)) & ","
    End If
    Next

    wait substring(numStr, 1, Len(numStr) - 1)

    Above code will display "1,3,4,5,7"

  5. #5
    Join Date
    Apr 2004
    Posts
    19

    1 more thing Jim

    Wow, Jim, thanks, I modified it a bit for foxpro below as you can see. 1 last question, some of the fields have numbers instead of the X's like 1 1 1 or 1 34 67
    How would I make it so that field would then read 1,3,4,6,7, basically just pluggin in commas and taking out the spaces ?
    Below is you're code I modified a bit for the X's. It worked great, if I can only get that last part with the records that have numbers in it I am set.

    scan
    ln = 0
    I = 0
    chr = ""
    numStr = ""
    s1 = purchspec

    ln = Len(s1)
    numStr = ""
    For I = 1 To ln Step 1
    chr = substr(s1, I, 1)
    If chr = "X" Then
    numStr = numStr + Trim(str(I)) + ","
    EndIf
    Next
    replac spec2 with strtran(substr(numStr, 1, Len(numStr) - 1),' ','')
    endscan

  6. #6
    Join Date
    Sep 2003
    Posts
    17
    I am bit confused with your reply, are you saying that you will have "X" and numbers i.e. 0-9 both in the same string. e.g. "X 1 456 XX" or you will have separate record for each one that will have only "X" in the string with space in one record and other record will only number in String. either way I am providing you sample code for All.

    1. Record has only "X" in the String.
    Example you have already.

    2. Record has only number in the String.

    scan
    ln = 0
    I = 0
    chr = ""
    numStr = ""
    s1 = purchspec

    ln = Len(s1)
    numStr = ""
    For I = 1 To ln Step 1
    chr = substr(s1, I, 1)
    If ASC(chr) >= 48 AND ASC(chr) <= 57 Then
    numStr = numStr + Trim(str(chr)) + ","
    EndIf
    Next
    replac spec2 with strtran(substr(numStr, 1, Len(numStr) - 1),' ','')
    endscan


    3. Record has both "X" and numbers in the String.

    scan
    ln = 0
    I = 0
    chr = ""
    numStr = ""
    s1 = purchspec

    ln = Len(s1)
    numStr = ""
    For I = 1 To ln Step 1
    chr = substr(s1, I, 1)
    If ASC(chr) >= 48 AND ASC(chr) <= 57 Then
    numStr = numStr + Trim(str(chr)) + ","
    ELSE
    If chr = "X" Then
    numStr = numStr + Trim(str(I)) + ","
    EndIf
    EndIf
    Next
    replac spec2 with strtran(substr(numStr, 1, Len(numStr) - 1),' ','')
    endscan

    By the way my name is "Jaymin" and nick name is "James" and I am from INDIA.

  7. #7
    Join Date
    Apr 2004
    Posts
    19

    foxpro command question

    Jaymin, thanks for the reply again! Do you go by Jim or Jaymin on these boards? So you are in India, wow, far away as I am in Philadelphia. Nice to meet you!
    Sorry to confuse you, I have fields that have either all "X"'s in them OR just Numbers like 123 56
    I tried to use the code you provided for the numbers and it didn't work. Not sure why as I tried to edit it as well but I had no luck.
    Any ideas ?
    Thanks again and nice to meet you...
    Rob

  8. #8
    Join Date
    Sep 2003
    Posts
    17
    Hi Rob,

    Could you please let me know what problem you are facing. Since I don't have foxpro install I have just gave you code that was not validated. However this time I am giving you working code for Visual Basic that you can easily read it and covert it to FoxPro code.

    Dim ln, I As Integer
    Dim s1 As String, chr As String
    Dim numStr As String
    s1 = "1 345 7"
    ln = Len(s1)
    numStr = ""
    For I = 1 To ln Step 1
    chr = Mid(s1, I, 1)
    If Asc(chr) >= 48 And Asc(chr) <= 57 Then
    numStr = numStr + Trim(Str(chr)) + ","
    Else
    If chr = "X" Then
    numStr = numStr + Trim(Str(I)) + ","
    End If
    End If
    Next
    MsgBox Mid(numStr, 1, Len(numStr) - 1)

    Just replace the value for s1 string variable with "X" values and it will work for both i.e. "X" as well as for numbers. Also, I am providing you the Foxpro code but again this is not tested. You may have to modify some code Foxpro.

    ln=0
    I=0
    s1=""
    chr=""
    numStr =""

    s1 = "1 345 7"
    ln = Len(s1)
    numStr = ""
    For I = 1 To ln Step 1
    chr = SubString(s1, I, 1)
    If Asc(chr) >= 48 And Asc(chr) <= 57 Then
    numStr = numStr + Trim(Str(chr)) + ","
    Else
    If chr = "X" Then
    numStr = numStr + Trim(Str(I)) + ","
    End If
    End If
    Next

    Wait SubString(numStr, 1, Len(numStr) - 1)

    Additionally, I am providing you my personal email address, just in case you need any help. My Name is Jaymin, My Id on this forum is shaja70 and I write my nick name as James.

    eMail: ShahJaymin0@yahoo.com

    Hope this helps,
    Thanks,
    James

  9. #9
    Join Date
    Aug 2004
    Posts
    28
    Here is another way you can accomplish this...


    * Created a table with one field to parse.
    SELECT Junk

    NewString = ""

    * Go through ALL the records in the table.
    SCAN

    * Evaluate each field that needs to be modified.
    FOR Looper = 1 TO LEN(TheField)
    TempString = SUBSTR(TheField, Looper, 1)
    IF TempString <> " "
    NewString = NewString + ALLTRIM(STR(Looper))
    ELSE
    NewString = NewString + TempString
    ENDIF
    ENDFOR
    * Commented this out because of possible issues I see (see below).
    * Replace Field2 with NewString
    ENDSCAN

    * just print the string to the FoxPro screen.
    ? NewString


    PROBLEMS :

    1.) This NEW String becomes very conveluted with data. ie.. once you get above the number 10 (position 10), each position starts to take up 2 positions in the new data.

    2.) because of this conveluted aspect, what good will the NEW data be ?


    AmcAmx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •