Delete part of text

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

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Delete part of text

Post by AlessioForconi » Fri Nov 16, 2018 5:27 pm

Hello,

trying to delete part of the contents of a variable I tried to use this code

Code: Select all

repeat for each line temp in tFiles
      put ".png" into textToDelete
      delete textToDelete of temp
      answer temp
end repeat
but it does not work.

The text I would like to delete is the extension (.png)

what am I doing wrong?

Thank you

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Delete part of text

Post by jmburnod » Fri Nov 16, 2018 5:40 pm

Hi Alessio,
You can use replace like this:

Code: Select all

replace ".png" with empty in tFiles
or use a loop

Code: Select all

    put empty into tFilesWithoutExtPng
   put ".png" into textToDelete
   repeat for each line temp in tFiles
      if char -4 to -1 of temp = textToDelete then
         delete char -4 to -1 of  temp
      end if 
      put temp & cr after tFilesWithoutExtPng
   end repeat
   delete char -1 of tFilesWithoutExtPng
   put tFilesWithoutExtPng
Best regards
Jean-Marc
https://alternatic.ch

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

Re: Delete part of text

Post by Klaus » Fri Nov 16, 2018 5:54 pm

Hi friends,

what Jean-Marc said:

Code: Select all

...
replace ".png" with EMPTY in tFiles
...
HINT:
"repeat for each xxx" is READ ONLY!
Means you cannot modify the currently computed line/item or whatever!
Example:

Code: Select all

...
repeat for each line tLine in tFiles
  ## You cannot change tLine!
  if tLine ends with ".png" then
  
     ## Does not work:
     ## delete char -4 to -1 of tLine
     put char 1 to -5 of tLine & CR after tNewList
  end if
end repeat
## get rid of trailing CR
delete char -1 of tNewList
...
Best

Klaus

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Delete part of text

Post by jmburnod » Fri Nov 16, 2018 6:08 pm

Hi Klaus
I had forgotten "ends with" which is useful for this.
Not sure I understand
repeat for each xxx" is READ ONLY!
line:

Code: Select all

delete char -4 to -1 of  temp
in my second script works fine. What I dont understand ?
Best
Jean-Marc
https://alternatic.ch

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

Re: Delete part of text

Post by Klaus » Fri Nov 16, 2018 6:23 pm

Hi Jean-Marc,

hm, maybe because you are not really using temp but copy it to another variable?
Works:

Code: Select all

...
  put fld 1 into tFiles
   put empty into tFilesWithoutExtPng
   put ".jpg" into textToDelete
   repeat for each line temp in tFiles
      if char -4 to -1 of temp = textToDelete then
         delete char -4 to -1 of  temp
      end if 
      ## !!!
      put temp & cr after tFilesWithoutExtPng
   end repeat
...
If you leave out the last line:

Code: Select all

...
  put fld 1 into tFiles
   put empty into tFilesWithoutExtPng
   put ".jpg" into textToDelete
   repeat for each line temp in tFiles
      if char -4 to -1 of temp = textToDelete then
         delete char -4 to -1 of  temp
      end if 
    ## !!!
    ##  put temp & cr after tFilesWithoutExtPng
   end repeat
...
then tFiles is NOT altered after the loop!
So to avoid surpises I always treat "repeat for each xxx" as completely read only! :D

Best

Klaus

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

Re: Delete part of text

Post by dunbarx » Fri Nov 16, 2018 6:47 pm

Klaus
I always treat "repeat for each xxx" as completely read only
I wonder if this is just how you are "saying" it.

Code: Select all

on mouseUp
   repeat for each item tItem in "1,2,3,4,5"
      multiply tItem by 10
      put tItem & return after temp
   end repeat
   answer temp
end mouseUp
Surely not just "read only". And you surely do this sort of thing. So, "read only"?? :wink:

Craig

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Delete part of text

Post by jmburnod » Fri Nov 16, 2018 6:48 pm

hm, maybe because you are not really using temp but copy it to another variable?
Yes Sir
Thanks again Klaus
https://alternatic.ch

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

Re: Delete part of text

Post by Klaus » Fri Nov 16, 2018 6:58 pm

dunbarx wrote:
Fri Nov 16, 2018 6:47 pm

Code: Select all

on mouseUp
   repeat for each item tItem in "1,2,3,4,5"
      multiply tItem by 10
      put tItem & return after temp
   end repeat
   answer temp
end mouseUp
Surely not just "read only". And you surely do this sort of thing. So, "read only"?
See my example and explanation for Jean-Marc above...
Same applies to your example.

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

Re: Delete part of text

Post by richmond62 » Fri Nov 16, 2018 7:45 pm

Slightly misleading that, "Delete part of text" when really:
delete part of the contents of a variable
NOT, however, in the end it should make much difference.

Code: Select all

on mouseUp
   put "XXX.png" into ZZZ
   if ZZZ contains ".png" then
      repeat for 4 times
      delete the last char of ZZZ
   end repeat
   end if
   put ZZZ
end mouseUp

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

Re: Delete part of text

Post by jacque » Sat Nov 17, 2018 6:00 pm

In earlier versions of LC you couldn't change the variable in the counter loop. That was changed some versions ago and now you (reportedly) can. The dictionary mentions this, but I've not tried it yet. Like Klaus, I've been too accustomed to the old way to take any chances.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Delete part of text

Post by Klaus » Sat Nov 17, 2018 7:48 pm

jacque wrote:
Sat Nov 17, 2018 6:00 pm
That was changed some versions ago and now you (reportedly) can.
Nope, still does not work, see my second posting here.

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

Re: Delete part of text

Post by dunbarx » Sun Nov 18, 2018 3:48 am

All.

I guess I am just not understanding.

Not the first time.

In "repeat for each...", the variable instance can be modified as often as desired. It lives apart from the loop construct. What is it that cannot be changed? What is it in the construction that "should" be "read only"

@Jacque. It sounded to me like your post referred to "repeat with..." where the index (I think) used to be sacrosanct. But now one can:

Code: Select all

on mouseUp
   repeat with y = 1 to 10
      put y into line y of temp
      if y = 5 then add 3 to y
   end repeat
   answer temp
end mouseUp
I do not think this would work in HC, but am not sure.

But in any case, I am missing the point. What "should not" be modified in "repeat with..."?

Craig

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

Re: Delete part of text

Post by jacque » Sun Nov 18, 2018 5:51 am

@Jacque. It sounded to me like your post referred to "repeat with..." where the index (I think) used to be sacrosanct.
You're right, my brain went off on its own again.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Re: Delete part of text

Post by AlessioForconi » Sun Nov 18, 2018 9:16 am

I'm not sure I fully understood all your reasoning, also because the language is not mine, but I solved it

Code: Select all

repeat for each line temp in tFiles
      put temp into temp2
      if temp2 contains ".png" then
         delete char -4 to -1 of  temp2
         if temp2 = currentDay then 
            set the filename of image "imgDay" to defaultFolder & "/days/" & temp
            exit repeat
         end if
      end if 
   end repeat
and it works perfectly, so thank you all.

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

Re: Delete part of text

Post by Klaus » Sun Nov 18, 2018 10:37 am

OK, OK, let me put it this way, I think this is also what the dictionary means.
In a repeat for each loop like this:

Code: Select all

...
repeat for each line temp in tFiles
         ## do whatever you want to with temp:
         delete char -4 to -1 of  temp
         ## if temp was -> my_image.jpg 
         ## then it now is in fact -> my_image
end repeat
...
You can modify temp and do with it whatever you like, but the "container" (tFiles in this case) will NOT change in the end, means tFiles BEFORE the loop = tFiles AFTER the loop.

It is always difficult to explain something like this in your non-native language.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”