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!
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
redfield
- Posts: 95
- Joined: Thu Apr 04, 2019 1:41 pm
Post
by redfield » Mon May 09, 2022 6:56 pm
Hi guys,
can't get this to work. What I have is a text file, containing data like this:
2021-06-0182.83
2021-07-0184.45
What I want is to replace the number values (second column) with new ones, e. g. 82.83 with 79.98. But what I get is:
2021-06-0182.83
2021-07-0184.45
2021-07-0179.98
2021-08-0182.02
2021-09-0178.95
So the new values are appended and I even get empty rows in between

. This is my code:
Code: Select all
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.
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Mon May 09, 2022 7:04 pm
Hi.
I assume you are doing all the massaging in LC before you write to the external file. So if you have your data in a variable "yourData":
2021-06-01 82.83
2021-07-01 84.45
How is the "second column" delimited from the first? A tab character between them? If so, did you:
Code: Select all
set the itemDel to tab
get yourData
put "79.98" into item 2 of line 1 of it
??
Craig
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Mon May 09, 2022 7:09 pm
Hi again.
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
This begs the issue whether the structure itself is sound.
Craig
-
redfield
- Posts: 95
- Joined: Thu Apr 04, 2019 1:41 pm
Post
by redfield » Mon May 09, 2022 8:22 pm
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:
Code: Select all
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...

-
Klaus
- Posts: 14190
- Joined: Sat Apr 08, 2006 8:41 am
-
Contact:
Post
by Klaus » Mon May 09, 2022 9:23 pm
Hi redfield,
two big issues!
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:
Code: Select all
...
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"
...
Best
Klaus
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Mon May 09, 2022 10:42 pm
OK.
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.
Code: Select all
repeat for each line l in it
...
put 100.0 into word 2 of l in it
end repeat
Cant do this. You can put 100.0 into word 2 of l, or you can put it into word 2 of it, but not into the pair of those two variables.
Your itemDelimiter needs to be what separates the two portions of each line, in your case, four spaces. So:
Code: Select all
set the itemDelimiter to " " --(four spaces)
I suspect this will still need tweaking, but that is what this forum is for.
Craig
-
redfield
- Posts: 95
- Joined: Thu Apr 04, 2019 1:41 pm
Post
by redfield » Sat May 14, 2022 4:29 pm
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:
Code: Select all
set the itemDelimiter to " " --(four spaces)
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.
-
Cairoo
- Posts: 112
- Joined: Wed Dec 05, 2012 5:54 pm
Post
by Cairoo » Mon May 16, 2022 5:39 pm
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.
Gerrie