Page 1 of 1
Remove item 1 in each line of txt
Posted: Thu Nov 29, 2012 8:11 pm
by snm
I need to remove item one in each line of text in variable. I made button with script:
Code: Select all
repeat for each line L in tText
delete item 1 of L
end repeat
In debugger I can see that line L is changing (first item is deleted), but tText doesn't change.
What can be wrong in my code?
Marek
Re: Remove item 1 in each line of txt
Posted: Thu Nov 29, 2012 9:07 pm
by Klaus
Hi Marek,
"repeat for each..." is insanely fast, but also READ ONLY, which means you cannot modify L (in your case)!
Do something like this:
Code: Select all
...
put fld "whatever holds tText" into tText
repeat for each line L in tText
## Get second to last item:
put item 2 to -1 of L & CR after tNewText
end repeat
## Get rid of trailing CR
delete char -1 of tNewText
put tNewText into fld "whatever holds tText"
...
Best
Klaus
Re: Remove item 1 in each line of txt
Posted: Thu Nov 29, 2012 9:25 pm
by jmburnod
Hi Marek,
Caramba ! Klaus has been faster than me one more.
And his script also: 25 milliseconds.
Code: Select all
on mouseup -- -- 1320 milliseconds
put the milliseconds into old
repeat with i = 1 to 10000
put i & "," & random(100) & cr after tText
end repeat
put 0 into tCount
repeat for each line L in tText
add 1 to tCount
-- delete item 1 of L
delete item 1 of Line tCount of tText
end repeat
put "TimeUsedMilliseconds = " && the milliseconds - old & cr & tText
end mouseup
on mouseup -- 10589 milliseconds
put the milliseconds into old
repeat with i = 1 to 10000
put i & "," & random(100) & cr after tText
end repeat
repeat with i = 1 to the num of lines of tText
delete item 1 of line i of tText
wait 1 milliseconds with messages
end repeat
put "TimeUsedMilliseconds = " && the milliseconds - old & cr & tText -- 10589
end mouseup
Best
Jean-Marc
Re: Remove item 1 in each line of txt
Posted: Thu Nov 29, 2012 10:05 pm
by snm
Thanks a lot Klaus and Jean-Marc.
I was not sure if "repeat for each L ..." Is bug or "red only" - I couldn't find the answer anywhere, so decided to ask you friends.
I started to runaround with Jean-Marc solution, but drop it as slow, asked here and gone home to eat something and go sleep. I was hope to find there solution tomorrow morning, didn't expect such quick answers.
Thanks a lot once more.
Marek
Re: Remove item 1 in each line of txt
Posted: Thu Nov 29, 2012 11:02 pm
by jmburnod
Hi Marek
was not sure if "repeat for each L ..." Is bug or "red only" - I couldn't find the answer anywhere
L is a variable that you can read but that has no effect on tText
Best
Jean-Marc
Re: Remove item 1 in each line of txt
Posted: Thu Nov 29, 2012 11:29 pm
by snm
Hi Jean-Marc,
It's a pity, but now i know that.
Marek
Re: Remove item 1 in each line of txt
Posted: Thu Nov 29, 2012 11:42 pm
by dunbarx
Hi.
As you have heard, the "repeat for each..." construction is fast.
What you tried to do, and conceptually it was sound, was appropriate for the "repeat with..." construction":
Code: Select all
on mouseUp
repeat with y = 1 to the number of lines of tText
delete item 1 of line y of tText
end repeat
answer tText
end mouseUp
This is straight out of your original intent, which again, was sound, but misapplied to one of LC's several possible repeat options.
Craig Newman