Having an issue using replace and Item number

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

Post Reply
DavJans
Posts: 270
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Having an issue using replace and Item number

Post by DavJans » Wed Jun 06, 2018 9:52 pm

Pretend that I have this data in tLine
1,2,3,4,5,6,3
unexpectedly I am getting this result
1,2,1,4,5,6,1

Code: Select all

   repeat for each line tLine in sortedxml
      put item 3 of tLine into tQT
      if tQT > " '1'" then
         replace item 3 of tLine with "1" in tLine
         repeat tQT
            if tempsortedxml is empty then
               put tLine into tempsortedxml
            else
               put cr & tLine after tempsortedxml
            end if
         end repeat
      else
         if tempsortedxml is empty then
            put tLine into tempsortedxml
         else
            put cr & tLine after tempsortedxml
         end if
      end if
   end repeat
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Having an issue using replace and Item number

Post by dunbarx » Wed Jun 06, 2018 11:10 pm

Hi.

This seems to work as advertised. A couple of comments:

What is this?"

Code: Select all

if tQT > " '1'" then
In other words, what is with the embedded single quotes? That comparison will always return "true", because the argument of the comparison is not a number. LC will interpret it as a silly string :D and just say yes.

Anyway, changing it to "1" lets the handler run "normally". The "3" is replaced in both items with a "1", as you asked. I do not have any information about the initial state of "tempsortedxml", so I cannot comment. But what is it that you are not comfortable with?

Craig Newman

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Having an issue using replace and Item number

Post by SparkOut » Thu Jun 07, 2018 6:48 am

Yes, what is the expected result?
If you are looking for 1,2,1,4,5,6,3 then "replace" is not what you need, as it will replace all instances of the matched value in tLine.
If you only want to replace item 3 then simply

Code: Select all

put 1 into item 3 of tLine
If it is not what you need, then we need to understand more about what it is that you need.

DavJans
Posts: 270
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Having an issue using replace and Item number

Post by DavJans » Tue Jun 19, 2018 12:02 am

So sorry guys, I have been on vacation. What SparkOut assumed I was looking for is correct, and his answer is what I needed, Also dunbarx, I have made that fix as well. Thank you both for your help.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

Klaus
Posts: 13820
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Having an issue using replace and Item number

Post by Klaus » Tue Jun 19, 2018 12:11 am

Important:
"repeat for each line tLine ib xxx" is READ ONLY, means you cannot modify tLine!
If you need to modify the content of tLine you will have to create new list:

Code: Select all

...
repeat for each line tLine in sortedxml
    put tLine into tLineReady2Modify
    replace item 3 with 1 in tLineReady2Modify
    ### more stuff here...
    ### and here...
   put tLineReady2Modify & CR after tNewList
end repeat
## get rid of trailing CR
delete char -1 of tNewList
## write tNewlist back into field or whatever it came from...
...
You get the picture...


Best

Klaus

DavJans
Posts: 270
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Having an issue using replace and Item number

Post by DavJans » Tue Jun 19, 2018 2:39 pm

Klaus,

I understand that editing tLine isn't saving back into sortedxml.

however It does let me edit tline and then dump tline into tempsortedxml.

Code: Select all

if tempsortedxml is empty then
               put tLine into tempsortedxml
            else
               put cr & tLine after tempsortedxml
            end if
after all is done I am dumping tempsortedxml back into sortedxml.

Is this a bad way to do it, I can most definitely change it.

Thank you
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Having an issue using replace and Item number

Post by jacque » Tue Jun 19, 2018 5:53 pm

That's actually a good way to do it, since some early benchmarking a while back showed that creating a new list is faster than modifying the old one. It's probably still true.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Having an issue using replace and Item number

Post by SparkOut » Tue Jun 19, 2018 7:25 pm

I doubt there would be any difference in measurable terms but you can eliminate the check for the empty temp var and just

Code: Select all

put tLine & cr after tempsortedxml
at the end just delete the last char to get rid of the trailing cr.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9358
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Having an issue using replace and Item number

Post by richmond62 » Wed Feb 23, 2022 9:22 am

Can you explain what your expected result is in a similar sort of way to the way you explained
how you got what you didn't want?
unexpectedly I am getting this result
1,2,1,4,5,6,1

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”