Find & Replace with Wildcard(s)

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
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Find & Replace with Wildcard(s)

Post by richmond62 » Wed Jan 22, 2025 4:20 pm

I have a large number of images-as-buttons that have a naming convention that goes like this: 118.png, and they are referred to in a cardScript like that.

Now those images-as-buttons look pretty shabby, so I have replaced them with less shabby images-as-buttons with a naming convention like this: b118.

Because I am a fairly lazy sort and do not want to go through a monster Switch statement line by name changing all the instances of 'XXX.png' (and that 'XXX' may vary from 2 to 6 characters) to 'bXXX', I wonder if there is a way to use the Find & Replace stack to do that automatically . . .

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Find & Replace with Wildcard(s)

Post by richmond62 » Wed Jan 22, 2025 5:25 pm

Well: no answers forthcoming.

While waiting, I didn't wait and did this instead in a button:

Code: Select all

on mouseup
   put the script of this card into fld "SKRIPT"
   put 1 into VOX
   repeat until word VOX of fld "SKRIPT" is empty
      if word VOX of fld "SKRIPT" contains ".png" then
         put word VOX of fld "SKRIPT" into VOXXX
         put VOXXX into fld "VOCKS"
         delete the second char of fld "VOCKS"
         put "b" after the first char of fld "VOCKS"
         delete char -2 of fld "VOCKS"
         delete char -2 of fld "VOCKS"
         delete char -2 of fld "VOCKS"
         delete char -2 of fld "VOCKS"
         put fld "VOCKS" into VOKS
         put (" " & VOKS) after word VOX in fld "SKRIPT"
         delete word VOX of fld "SKRIPT"
      end if
      add 1 to VOX
   end repeat
   set the script of this card to fld "SKRIPT"
end mouseup
Really fairly tedious.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Find & Replace with Wildcard(s)

Post by richmond62 » Wed Jan 22, 2025 5:47 pm

This did NOT do what it was supposed to do:
-
Screenshot 2025-01-22 at 18.37.23.png
-
So I modified the script:

Code: Select all

on mouseup
   put the script of this card into fld "SKRIPT"
   put 1 into VOX
   repeat until word VOX of fld "SKRIPT" is empty
      if word VOX of fld "SKRIPT" contains ".png" then
         put word VOX of fld "SKRIPT" into VOXXX
         put VOXXX into fld "VOCKS"
         delete the first char of fld "VOCKS"
         put (quote & "b") before fld "VOCKS"
         delete char -2 of fld "VOCKS"
         delete char -2 of fld "VOCKS"
         delete char -2 of fld "VOCKS"
         delete char -2 of fld "VOCKS"
         put fld "VOCKS" into VOKS
         put (" " & VOKS) after word VOX in fld "SKRIPT"
         delete word VOX of fld "SKRIPT"
      end if
      add 1 to VOX
   end repeat
   set the script of this card to fld "SKRIPT"
end mouseup

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Find & Replace with Wildcard(s)

Post by Klaus » Wed Jan 22, 2025 6:24 pm

Hi Richmond,

can you please show us how the imagenames are stored in your script?
And how are they "managed", sequential or random numbers?

Important hint:
Put the content of a field into a variable first, work with that variable and you will experience a HEAVY speed bump! 8)
Accessing fields is "expensive", especially in a loop.

Best

Klaus

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Find & Replace with Wildcard(s)

Post by richmond62 » Wed Jan 22, 2025 6:46 pm

The HEAVY speed bump was not important as it was a 'once off'.

The images are apparently fairly random: here's a selection of them:

257.png
240.png
42961.png
-
42961.png
42961.png (12.26 KiB) Viewed 8550 times
-
and their names in the cardScript have to be changed to the following:

b257
b240
b42961

as they have been replaced with images-as-buttons with those names.

BUT as I have already achieved that with the script I have posted above, my question is largely redundant.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Find & Replace with Wildcard(s)

Post by dunbarx » Wed Jan 22, 2025 7:39 pm

Richmond.

Wouldn't it have been easier to cycle through all the .png names and:

Code: Select all

   set the itemDel to "png"
   repeat  with y = 1 to the number of imgages
      set the name of image y to "b" & item 1 of the short name of image y
   end repeat
Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Find & Replace with Wildcard(s)

Post by dunbarx » Wed Jan 22, 2025 8:01 pm

Or if there are more images of other types with other types of names:

Code: Select all

set the itemDel to "png"
   repeat  with y = 1 to the number of imgs
      if item 1 of the short name of img y is an integer then set the name of img y to "b" & item 1 of the short name of img y
   end repeat
That sort of thing...

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10078
Joined: Fri Feb 19, 2010 10:17 am

Re: Find & Replace with Wildcard(s)

Post by richmond62 » Wed Jan 22, 2025 8:17 pm

Wouldn't it have been easier to cycle through all the .png names and:
It probably would, but I was in a hurry, no-one was holding my hand, and my mentality is stuck in about 1984. 8)

Post Reply