Results 1 to 22 of 22

Thread: Change in specific position in .txt

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Change in specific position in .txt

    Hello everybody

    If i have two .txt files
    First contains a list of words each in new line :
    Similar to this
    One
    This
    Hello
    Two

    Second contains a long text:
    a sample part of the second file:

    INPUT STRING:
    LOOK-UP WORD: tnZm
    SOLUTION 1: (tanaZ~ama) [tanaZ~am_1] tanaZ~am/VERB_PERFECT+a/PVSUFF_SUBJ:3MS
    (GLOSS): + be organized/be regulated + he/it <verb>
    SOLUTION 2: (tanoZim) [naZam-i_1] ta/IV3FS+noZim/VERB_IMPERFECT
    (GLOSS): it/they/she + arrange/organize/compose +
    SOLUTION 3: (tunaZ~im) [naZ~am_1] tu/IV3FS+naZ~im/VERB_IMPERFECT
    (GLOSS): it/they/she + arrange/organize/regulate +

    INPUT STRING:
    LOOK-UP WORD: jAmEp
    SOLUTION 1: (jAmiEap) [jAmiE_1] jAmiE/NOUN+ap/NSUFF_FEM_SG
    (GLOSS): + comprehensive/extensive + [fem.sg.]
    SOLUTION 2: (jAmiEap) [jAmiEap_1] jAmiE/NOUN+ap/NSUFF_FEM_SG
    (GLOSS): + university/league + [fem.sg.]


    I need to replace each word between the brackets in the second file with the words in the first file , except the word (GLOSS) will not be changed

    Means that the word the second file will be changed to

    INPUT STRING:
    LOOK-UP WORD: tnZm
    SOLUTION 1: (one) [tanaZ~am_1] tanaZ~am/VERB_PERFECT+a/PVSUFF_SUBJ:3MS
    (GLOSS): + be organized/be regulated + he/it <verb>
    SOLUTION 2: (this) [naZam-i_1] ta/IV3FS+noZim/VERB_IMPERFECT
    (GLOSS): it/they/she + arrange/organize/compose +
    SOLUTION 3: (Hello) [naZ~am_1] tu/IV3FS+naZ~im/VERB_IMPERFECT
    (GLOSS): it/they/she + arrange/organize/regulate +

    INPUT STRING:
    LOOK-UP WORD: jAmEp
    SOLUTION 1: (two) [jAmiE_1] jAmiE/NOUN+ap/NSUFF_FEM_SG
    (GLOSS): + comprehensive/extensive + [fem.sg.]


    how can i change this code to help me:
    VB Code:
    1. Open "C:\second.txt" For Input As #1
    2.         tmp = Split(Input(LOF(1), 1), vbCrLf)
    3.     Close #1
    4.    
    5.    For X = 0 To UBound(tmp)        
    6.         If UCase(Left(tmp(X), 10)) = "  SOLUTION" Then            
    7.             Words = Split(tmp(X), "(")
    8.      
    9.        'what i need then

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    easy to do,
    open first file and split, same way as second file
    use instr to find position of "(" and again to find position of ")"
    then use mid to join the part upto and including "(", & the replacement from the first file array & mid again the part from and including ")"
    advance the index for the array from first file so that it will put the next string each time
    lastly use join for the array of lines, open the second file again, this time for output then print the joined string to the file and close
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    sure
    it should be easy to a programmer like you

    but in my case as a mother of 3 kids
    in addition to this that i didn't write any code since 2000

    i think that it's not easy for me to write the code

    OK i can understand what u wrote but i forgot most of the key word in VB and how to write the code
    could you please help me with that

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    try like this, but it isn't tested
    if you want to write program you got to learn code again

    VB Code:
    1. Dim arrfirst() As String, arrsecond() As String, i As Long, j As Long
    2. Dim pos As Long, pos1 As Long
    3. f1 = FreeFile
    4. Open "first.txt" For Input As f1  ' add path to file
    5.     arrfirst = split(Input(LOF(f1), #f1), vbNewLine)
    6.     Close f1
    7.  
    8. Open "second.txt" For Input As f1
    9.     arrsecond = split(Input(LOF(f1), #f1), vbNewLine)
    10.     Close f1
    11. j = 0
    12. For i = 0 To UBound(arrsecond)
    13.     If Left(arrsecond(i), 10) = "  SOLUTION" Then   ' from your post though don't look quite right
    14.         pos = InStr(arrsecond(i), "(") 'start position
    15.         pos1 = InStr(arrsecond(i), ")")  ' end position
    16.         arrsecond(i) = Mid(arrsecond(i), 1, pos) & arrfirst(j) & Mid(arrsecond(i), pos1, Len(arrsecond(i)) - pos1 + 1)
    17.         j = j + 1
    18.        
    19.     End If
    20.    
    21. Next
    22. Open "second.txt" For Output As f1   'overwrite updated data to file
    23.     Print #f1, join(arrsecond, vbNewLine)
    24.     Close f1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    it point to this line and gives an error
    "subscript out of range"

    VB Code:
    1. arrsecond(i) = Mid(arrsecond(i), 1, pos) & arrfirst(j) & Mid(arrsecond(i), pos1, Len(arrsecond(i)) - pos1 + 1)
    what's the problem


    by the way
    thanks for your advice
    i will try my best to be a good programmer as i was befor

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    i or j has gone past it's valid value
    i probably can't do, so it is most likly j, this will happen if more replacements are needed than in first file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Change in specific position in .txt

    You need to check upper bound of both arrays.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim arrfirst() As String
    5. Dim arrsecond() As String
    6. Dim f1 As Integer
    7. Dim cnt As Long
    8.  
    9.    f1 = FreeFile:   Open "C:\first.txt" For Input As #f1  ' update file path
    10.       arrfirst = Split(Input(LOF(f1), #f1), vbCrLf)
    11.    Close f1
    12.  
    13.    f1 = FreeFile:   Open "C:\second.txt" For Input As #f1
    14.       arrsecond = Split(Input(LOF(f1), #f1), vbCrLf & "SOLUTION ")
    15.    Close f1
    16.    
    17.    cnt = 1
    18.    Do
    19.       arrsecond(cnt) = Left(arrsecond(cnt), InStr(1, arrsecond(cnt), "(")) _
    20.          & arrfirst(cnt - 1) & Mid(arrsecond(cnt), InStr(1, arrsecond(cnt), ")"))
    21.       cnt = cnt + 1
    22.    Loop Until cnt - 1 > UBound(arrfirst) Or cnt > UBound(arrsecond)
    23.    
    24.    f1 = FreeFile:   Open "C:\result.txt" For Output As #f1
    25.       Print #f1, Join(arrsecond, vbCrLf & "SOLUTION ")
    26.    Close #f1
    27. End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    i tried that westconn1
    again
    but it do a replacement with no relation to the first file
    i mean it put some unknown words i don't know why?

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Change in specific position in .txt

    Giving an example of the errorneus results might help the person who tries to help

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    i have no idea how it can make a replacement that is not in the list, post your complete code, also the text files if possible so i can try it
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    i hope u can help me with this because the first file is written in arabic
    newoutfile.txt is the first file
    and outfile.txt is the second file that i need to change

    i need to put the code as a function
    Attached Files Attached Files

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    i tried this code for another similar file but this code replaces each letter
    with another one
    can i change it to be helpful in my case

    VB Code:
    1. Dim pos1 As Long, pos2 As Long, i As Long
    2. Dim file As String, line As String, word As String
    3.     file = vbNullString
    4.     i = 0
    5.         Open "C:\buckwalter_morphan_1\data\outfile.txt" For Input As #1
    6.             Do While Not EOF(1)
    7.                 Line Input #1, line
    8.                     i = i + 1
    9.                     pos1 = InStr(1, line, "(")
    10.                     pos2 = InStr(1, line, ")")
    11.                         If pos1 > 0 And pos2 > 0 Then
    12.                             word = Mid(line, pos1 + 1, pos2 - pos1 - 1)
    13.                            
    14.                                 word = Replace(word, "'", "Á")
    15.                                 word = Replace(word, "|", "Â")
    16.                  
    17.                                 '...
    18.                                 'define all replacements up to letters(25)
    19.                                
    20.                             line = Left(line, pos1 - 1) & "(" & word & ")" & Right(line, Len(line) - pos2)
    21.                         End If
    22.                             If i = 1 Then
    23.                                 file = line
    24.                             Else
    25.                                 file = file & vbCrLf & line
    26.                             End If
    27.             Loop
    28.         Close #1
    29.        
    30.             Open "C:\buckwalter_morphan_1\data\outfile.txt" For Output As #1
    31.                 Print #1, file
    32.             Close #1
    33.                 MsgBox "Done!"

  13. #13
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Change in specific position in .txt

    Haha... that's my code from few mounths ago!

    How would you like to change it?

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    Yes
    Hi gavio that's your code

    this code is replaceing each letter between the brackets with another letter
    according to the replacements that i add

    so this code is applying the same method to all brackets


    what i need is to replace the first word between brackets found in the first file with the first word in the second file and so one.....

    (the replacement should not applied in case of in the word between brackets was (GLOSS))

    YOU CAN READ PRVIOUS THREADS


    I'M SURE THAT YOU CAN DO IT

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    i downloaded your textfiles and ran the code as i posted above, i just copied from the post, changed the file names, it gave no errors and appeared to work right
    here is the output file

    Edit: you were quite right, i could not understand the arabic at all, which is why i say appeared to work, you can check it out
    Attached Files Attached Files
    Last edited by westconn1; Nov 2nd, 2006 at 03:58 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    i saw the file westconn1
    i got the same output
    and this is what i got between the bracets which is not understandable

    ط§ظ„ط¬ظژظ…ظ’ط¹ظگظٹظ‘ظژط©

    whats the problem ??????!!!!!

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    some of the replacement text (the arabic) is unicode, i don't know too much about unicode, but read this post here http://www.vbforums.com/showthread.php?t=365738, about unicode in vb6, there will be many other posts on converting to unicode, vb automaically converts the unicode from the file to ANSI as soon as it reads the file, if you read the file as a byte array you then have the values to get the unicode character from the 2 bytes that make the unicode character, but i am unable to tell you how to put the unicode into your string and save it.

    after typing all this i just made it work so try this code
    VB Code:
    1. Sub om()
    2.  
    3. Dim arrfirst() As Byte, arrsecond() As String, i As Long, j As Long
    4. Dim pos As Long, pos1 As Long, strt As Long, arra() As Byte, arrb() As Byte
    5. f1 = FreeFile
    6. Open "C:\Documents and Settings\peter\My Documents\newoutfile.txt" For Input As f1  ' add path to file
    7.     arrfirst = Input(LOF(f1), #f1)
    8.     Close f1
    9.  
    10. For i = 0 To UBound(arrfirst)
    11.     Debug.Print arrfirst(i)
    12. Next
    13. Open "C:\Documents and Settings\peter\My Documents\outfile.txt" For Input As f1
    14.     arrsecond = split(Input(LOF(f1), #f1), vbNewLine)
    15.     Close f1
    16. Open "C:\Documents and Settings\peter\My Documents\testoutfile.txt" For Output As f1   'overwrite updated data to file
    17. strt = 0
    18.  
    19. For i = 0 To UBound(arrsecond)
    20.     If Left(arrsecond(i), 10) = "  SOLUTION" Then   ' from your post though don't look quite right
    21.         pos = InStr(arrsecond(i), "(") 'start position
    22.         pos1 = InStr(arrsecond(i), ")")  ' end position
    23. rpl = getnext(arrfirst, strt)
    24. strt = strt + (Len(rpl) * 2) + 2
    25. For n = 0 To UBound(rpl)
    26. Next
    27.         arra = Mid(arrsecond(i), 1, pos)
    28.         arrb = Mid(arrsecond(i), pos1, Len(arrsecond(i)) - pos1 + 1)
    29.     Print #f1, arra; rpl; arrb
    30.         j = j + 1
    31.        
    32.     End If
    33.     Next
    34.     Close f1
    35. End Sub
    36. Function getnext(arri() As Byte, strt As Long)
    37. Dim i As Long, arrb() As Byte
    38. ReDim arrb(100)
    39. For i = strt To UBound(arri)
    40.  
    41.     If arri(i) = 13 Then
    42.     Exit For
    43.     Else
    44.         arrb(i - strt) = arri(i)
    45.     End If
    46. Next
    47. ReDim Preserve arrb(i - strt - 1)
    48. getnext = arrb
    49. End Function
    i am not sayin the coding is perfect, but try it out and see if it is correct, i also attatch the result file
    Attached Files Attached Files
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    very good it's correct

    but i need to overwrite in the outfile not in testoutfile
    i mean without extracting the solutions lines and put them in other file
    i need all information and the same arrangment of outfile
    because most of my codes depends on outfile.txt arrangment

    thanks

  19. #19
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    i just change it to a separate output file, so i could run the code again without keep recopying the files after they had been overwritten
    just change the filename for output back to the one you want, it makes no difference

    as i said i am not sure that this is the proper way to code, so someone might put up some improvments
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: Change in specific position in .txt

    no i think that you did't understand i need to keep all information in the outfile ... if i just change the files names i will lose some data that i need in other functoins in outfile.txt

    i attached a sample of output with all information i need which i didn't got in file outfile.txt

    and the second one (outfile2.txt) is what i'm getting know
    Attached Files Attached Files

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    in the previous, i think the first arabic word was incorrect, but that was what was making the it show the arabic words

    here is code i think will do all you require, as far as i can tell it is correct

    VB Code:
    1. Sub om()
    2.  
    3. Dim arrfirst() As Byte, arrsecond() As String, i As Long
    4. Dim pos As Long, pos1 As Long, strt As Long
    5. Dim bom() As Byte
    6. f1 = FreeFile
    7. ReDim arrfirst(FileLen("C:\Documents and Settings\peter\My Documents\newoutfile.txt") - 1)
    8. Open "C:\Documents and Settings\peter\My Documents\newoutfile.txt" For Input As f1  ' add path to file
    9.     arrfirst = Input(LOF(f1), #f1)  ' file containing arabic replacements into byte array
    10.     Close f1
    11.    
    12.  
    13. Open "C:\Documents and Settings\peter\My Documents\outfile.txt" For Input As f1
    14.     arrsecond = split(Input(LOF(f1), #f1), vbNewLine) ' string array of file needing replacements
    15.     Close f1
    16. Open "C:\Documents and Settings\peter\My Documents\testoutfile.txt" For Output As f1   'overwrite updated data to file
    17. ReDim bom(5)
    18. For i = 0 To 5 'read BOM from file
    19.     bom(i) = arrfirst(i)
    20. Next
    21. Print #f1, bom;
    22. strt = 6 ' start after BOM
    23.  
    24. For i = 0 To UBound(arrsecond)
    25.     If Left(arrsecond(i), 10) = "  SOLUTION" Then   ' find line for replace
    26.         pos = InStr(arrsecond(i), "(") 'start position
    27.         pos1 = InStr(arrsecond(i), ")")  ' end position
    28. rpl = getnext(arrfirst, strt) ' to get the first line from the byte array
    29. strt = strt + (Len(rpl) * 2) + 4  ' next starting position in byte array
    30.        
    31.   ' print the line to file, with replacement
    32.     Print #f1, Mid(arrsecond(i), 1, pos); rpl; Mid(arrsecond(i), pos1, Len(arrsecond(i)) - pos1 + 1) ' print the line to file, with replacement
    33.         Else
    34.         Print #f1, arrsecond(i)  '; rpl
    35.     End If
    36.     Next
    37.     Close f1
    38. End Sub
    39. Function getnext(arri() As Byte, strt As Long)
    40. Dim i As Long, arrb() As Byte
    41. ReDim arrb(100)
    42. For i = strt To UBound(arri)
    43.  
    44.     If arri(i) = 13 Then
    45.     Exit For
    46.     Else
    47.         arrb(i - strt) = arri(i)
    48.     End If
    49. Next
    50. ReDim Preserve arrb(i - strt - 1)
    51. getnext = arrb
    52. End Function
    Last edited by westconn1; Nov 6th, 2006 at 04:49 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  22. #22
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change in specific position in .txt

    here is a simpler code that does not use byte array, except to write BOM
    VB Code:
    1. Sub om1()
    2.  
    3. Dim arrfirst() As String, arrsecond() As String, i As Long
    4. Dim pos As Long, pos1 As Long, j As Long
    5. Dim bom() As Byte
    6. f1 = FreeFile
    7. Open "C:\Documents and Settings\peter\My Documents\newoutfile.txt" For Input As f1  ' add path to file
    8.     arrfirst = split(Input(LOF(f1), #f1), vbNewLine) ' file containing arabic replacements into byte array
    9.     Close f1
    10.    
    11.  
    12. Open "C:\Documents and Settings\peter\My Documents\outfile.txt" For Input As f1
    13.     arrsecond = split(Input(LOF(f1), #f1), vbNewLine) ' string array of file needing replacements
    14.     Close f1
    15. Open "C:\Documents and Settings\peter\My Documents\testoutfile.txt" For Output As f1   'overwrite updated data to file
    16. ReDim bom(5)
    17. bom(0) = &HEF
    18. bom(2) = &HBB
    19. bom(4) = &HBF
    20. Print #f1, bom;
    21.  
    22. For i = 0 To UBound(arrsecond)
    23.     If Left(arrsecond(i), 10) = "  SOLUTION" Then   ' find line for replace
    24.         pos = InStr(arrsecond(i), "(") 'start position
    25.         pos1 = InStr(arrsecond(i), ")")  ' end position
    26.        
    27.   ' print the line to file, with replacement
    28.     Print #f1, Mid(arrsecond(i), 1, pos); arrfirst(j); Mid(arrsecond(i), pos1, Len(arrsecond(i)) - pos1 + 1) ' print the line to file, with replacement
    29.     j = j + 1
    30.         Else
    31.         Print #f1, arrsecond(i)  '; rpl
    32.     End If
    33.     Next
    34.     Close f1
    35. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width