Page 1 of 1

Having an issue using replace and Item number

Posted: Wed Jun 06, 2018 9:52 pm
by DavJans
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

Re: Having an issue using replace and Item number

Posted: Wed Jun 06, 2018 11:10 pm
by dunbarx
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

Re: Having an issue using replace and Item number

Posted: Thu Jun 07, 2018 6:48 am
by SparkOut
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.

Re: Having an issue using replace and Item number

Posted: Tue Jun 19, 2018 12:02 am
by DavJans
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.

Re: Having an issue using replace and Item number

Posted: Tue Jun 19, 2018 12:11 am
by Klaus
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

Re: Having an issue using replace and Item number

Posted: Tue Jun 19, 2018 2:39 pm
by DavJans
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

Re: Having an issue using replace and Item number

Posted: Tue Jun 19, 2018 5:53 pm
by jacque
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.

Re: Having an issue using replace and Item number

Posted: Tue Jun 19, 2018 7:25 pm
by SparkOut
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.

Re: Having an issue using replace and Item number

Posted: Wed Feb 23, 2022 9:22 am
by richmond62
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