SuperCard 4.8 Replicate String Utility Function

Need help with a script? This is the place to discuss how to get your code running!

SuperCard 4.8 Replicate String Utility Function

Postby codegreen » Wed Mar 30, 2016 7:19 am

SuperCard 4.8 adds a handy new string utility function called replicate.

As the name suggests, it returns zero or more copies of whatever prototype string you pass it:

Code: Select all
   put replicate(abc, 1)          -- abc
   put replicate(abc, 2)          -- abcabc
   put replicate(abc, 0)          --
   put replicate("abc def ", 2)   -- abc def abc def

You only fully appreciate the value of this when you have to generate something absurdly loooong like the 512 zeros at the start of a PICT file, but even for much shorter strings it's far less error-prone.

Which of these is easier to grok (and thus in which would you be more likely to spot an error)?

Code: Select all
   put replicate(0, 38) into x

or

Code: Select all
   put "00000000000000000000000000000000000000" into x

It's also useful for padding strings to a desired length:

Code: Select all
on mouseUp
   get random(100000000)
   put replicate(0, 10 - len(it)) & it
end mouseUp

Enjoy!
-Mark
codegreen
 
Posts: 1510
Joined: Mon Jul 14, 2008 11:03 pm

Return to Scripting in SuperTalk

Who is online

Users browsing this forum: No registered users and 1 guest

cron