Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
put specialFolderPath("resources") & "/" & "test.txt" into vPath
open file specialFolderPath("resources") & "/" & "test.txt" for update
read from file specialFolderPath("resources") & "/" & "test.txt" until EOF
repeat for each word i in it
if i contains "." then
put theNewValue into n
replace i with n in it
end if
end repeat
write it to file specialFolderPath("resources") & "/" & "test.txt"
close file specialFolderPath("resources") & "/" & "test.txt"
Help appreciated
Last edited by redfield on Sat May 14, 2022 4:29 pm, edited 1 time in total.
If you read your handler, you can see that for someone like me, I have little knowledge of how it works. I do not see the variable "theNewValue", for example. I do see that you are selecting lines that contain a period, but it seems so far that all lines do.
That is why I asked what the delimiter is. And the fact that you are getting empty rows is telling. Anyway, I bet that there is a simple solution, and you will get this going soon.
Little things, like you do not need to use the extra line that introduces the "n". You can just lose that. Just
Thanks Craig, for trying to understand my issue. The text file is actually created and filled by a Python script. The delimiter between the two "columns" are four spaces. The code now looks like this:
put specialFolderPath("resources") & "/" & "test.txt" into vPath
open file vPath for update
read from file vPath until EOF
set the itemDel to tab
repeat for each line l in it
answer word 2 of l
put 100.0 into word 2 of l in it
end repeat
write it to file specialFolderPath("resources") & "/" & "test.txt"
close file specialFolderPath("resources") & "/" & "test.txt"
In the loop the "answer" is as expected. But trying to put 100.0 into word 2 throws an error: "can't find handler) near "in",".
I am puzzled...
1. Only use IT when neccessary and as little as possible, IT wil change when you least expect IT!
Put IT immediately into a variable and work with that variable!
2. "repeat for each..." is READ-ONLY!
Means the container will not change or may show unexpected results as you have seen with your first script!
Create/fill a new variable with a "repeat for each..." loop lile this:
...
put specialFolderPath("resources") & "/test.txt" into vPath
## open file vPath for update
## read from file vPath until EOF
## I always use the URL one-liner!
put url("file:" & vPath) into tText
set the itemDel to tab
repeat for each line l in tText
## answer word 2 of l
## Use ITEM, since you have set the itemdelimiter above!
put 100.0 into item 2 of l
## put 100.0 into word 2 of l
put l & CR after tNewText
end repeat
## Get rid of trailing CR
delete char -1 of tNewText
## URL one-liner:
put tNewText into url("file:" & vPath)
## write it to file specialFolderPath("resources") & "/" & "test.txt"
## close file specialFolderPath("resources") & "/" & "test.txt"
...
Takes just a little practice to get going. One thing about the english-like syntax of LC is that new users believe they can just "talk" when building their code. They can, but there is still a strict syntax that you need to get used to.
Klaus wrote: Mon May 09, 2022 9:23 pm
1. Only use IT when neccessary and as little as possible, IT wil change when you least expect IT!
Put IT immediately into a variable and work with that variable!
Okay will try to remember.
Klaus wrote: Mon May 09, 2022 9:23 pm
2. "repeat for each..." is READ-ONLY!
Wow... good to know!
dunbarx wrote: Mon May 09, 2022 10:42 pm
Your itemDelimiter needs to be what separates the two portions of each line, in your case, four spaces. So:
Thanks, my code works well now, although I'm working with "word" instead of "item", which seems to be sufficient in this case, therefore I don't even need to use an itemdelimiter.
I'm not sure if it's really what you wanted, but the code seems to replace EVERY number in the second column with the same new number.
If that's what you wanted, then good. But if you wanted to replace a particular number like what your original post seemed to suggest, you may have to make some additional changes.