The Ring programming language version 1.9 book - Part 43 of 210
1. Ring Documentation, Release 1.9
Syntax:
Endswith(string, substring) ---> True/False
Example:
Load "stdlib.ring"
Puts("Test Endswith()")
see endsWith("CalmoSoft", "Soft") + nl
47.28 GCD() function
Finding of the greatest common divisor of two integers.
Syntax:
Gcd(number,number) ---> number
Example:
Load "stdlib.ring"
Puts("Test Gcd()")
see gcd (24, 32) + nl
47.29 LCM() function
Compute the least common multiple of two integers.
Syntax:
lcm(number,number) ---> number
Example:
Load "stdlib.ring"
Puts("Test Lcm()")
see Lcm(24,36) + nl
47.30 SumList() function
Compute the sum of a list of integers.
Syntax:
sumlist(list) ---> number
Example:
Load "stdlib.ring"
Puts("Test Sumlist()")
47.28. GCD() function 389
2. Ring Documentation, Release 1.9
aList = [1,2,3,4,5]
see Sumlist(aList) + nl
47.31 ProdList() function
Compute the product of a list of integers.
Syntax:
prodlist(list) ---> number
Example:
Load "stdlib.ring"
Puts("Test Prodlist()")
aList = [1,2,3,4,5]
see Prodlist(aList) + nl
47.32 EvenOrOdd() function
Test whether an integer is even or odd.
Result of test (1=odd 2=even).
Syntax:
evenorodd(number) ---> 1 (odd) or 2 (even)
Example:
Load "stdlib.ring"
Puts("Test Evenorodd()")
nr = 17
see Evenorodd(nr) + nl
47.33 Factors() function
Compute the factors of a positive integer.
Syntax:
factors(number) ---> list
Example:
Load "stdlib.ring"
Puts("Test Factors()")
n = 45
aList = factors(n)
see "Factors of " + n + " = "
for i = 1 to len(aList)
47.31. ProdList() function 390
3. Ring Documentation, Release 1.9
see "" + aList[i] + " "
next
47.34 Palindrome() function
Check if a sequence of characters is a palindrome or not.
Syntax:
Palindrome(String) ---> True/False
Example:
Load "stdlib.ring"
Puts("Test Palindrome()")
cString = "radar"
see Palindrome(cString)
47.35 IsLeapYear() function
Check whether a given year is a leap year in the Gregorian calendar.
Syntax:
Isleapyear(number) ---> True/False
Example:
Load "stdlib.ring"
Puts("Test Isleapyear()")
year = 2016
if Isleapyear(year) see "" + year + " is a leap year."
else see "" + year + " is not a leap year." ok
47.36 BinaryDigits() function
Compute the sequence of binary digits for a given non-negative integer.
Syntax:
binarydigits(number) ---> string
Example:
Load "stdlib.ring"
Puts("Test Binarydigits()")
b = 35
see "Binary digits of " + b + " = " + Binarydigits(b)
47.34. Palindrome() function 391
4. Ring Documentation, Release 1.9
47.37 MatrixMulti() function
Multiply two matrices together.
Syntax:
Matrixmulti(List,List) ---> List
Example:
Load "stdlib.ring"
# Multiply two matrices together.
Puts("Test Matrixmulti()")
A = [[1,2,3], [4,5,6], [7,8,9]]
B = [[1,0,0], [0,1,0], [0,0,1]]
see Matrixmulti(A, B)
47.38 MatrixTrans() function
Transpose an arbitrarily sized rectangular Matrix.
Syntax:
Matrixtrans(List) ---> List
Example:
Load "stdlib.ring"
# Transpose an arbitrarily sized rectangular Matrix.
Puts("Test Matrixtrans()")
matrix = [[78,19,30,12,36], [49,10,65,42,50], [30,93,24,78,10], [39,68,27,64,29]]
see Matrixtrans(matrix)
47.39 DayOfWeek() function
Return the day of the week of given date. (yyyy-mm-dd)
Syntax:
dayofweek(string) ---> string
Example:
Load "stdlib.ring"
# Return the day of the week of given date.
Puts("Test Dayofweek()")
date = "2016-04-24"
see "Data : " + date + " - Day : " + Dayofweek(date) + nl
47.37. MatrixMulti() function 392
5. Ring Documentation, Release 1.9
47.40 Permutation() function
Generates all permutations of n different numerals.
Syntax:
permutation(list)
Example:
Load "stdlib.ring"
# Generates all permutations of n different numerals
Puts("Test Permutation()")
list = [1, 2, 3, 4]
for perm = 1 to 24
for i = 1 to len(list)
see list[i] + " "
next
see nl
Permutation(list)
next
47.41 ReadLine() function
Read line from file
Syntax:
readline(fp) ---> string
Example:
Load "stdlib.ring"
# Read a file line by line.
Puts("Test Readline()")
fp = fopen("test.ring","r")
while not feof(fp)
See Readline(fp) end
fclose(fp)
47.42 SubString() function
Return a position of a substring starting from a given position in a string.
Syntax:
Substring(str,substr,npos) ---> string
Example:
Load "stdlib.ring"
# Return a position of a substring starting from a given position in a string.
47.40. Permutation() function 393
6. Ring Documentation, Release 1.9
Puts("Test Substring()")
a = "abcxyzqweabc"
b = "abc"
i = 4
see substring(a,b,i)
47.43 ChangeString() function
Change substring from given position to a given position with another substring.
Syntax:
Changestring(cString, nPos1, nPos2, cSubstr) ---> cString
Example:
Load "stdlib.ring"
# Change substring from given position for given position with a substring.
Puts("Test Changestring()")
see Changestring("Rmasdg",2,5,"in") # Ring
47.44 Sleep() function
Sleep for the given amount of time.
Syntax:
sleep(nSeconds)
Example:
Load "stdlib.ring"
Puts("Test Sleep()")
see "Wait 3 Seconds!"
Sleep(3)
see nl
47.45 IsMainSourceFile() function
Check if the current file is the main source file
Syntax:
IsMainSourceFile() ---> True/False
Example:
Load "stdlib.ring"
if ismainsourcefile()
# code
ok
47.43. ChangeString() function 394
7. Ring Documentation, Release 1.9
47.46 DirExists() function
Check if directory exists
Syntax:
DirExists(String) ---> True/False
Example:
Load "stdlib.ring"
see "Check dir : b:ring "
puts( DirExists("b:ring") )
see "Check dir : C:ring "
Puts( DirExists("C:ring") )
47.47 MakeDir() function
Make Directory
Syntax:
MakeDir(String)
Example:
Load "stdlib.ring"
# Create Directory
puts("create Directory : myfolder")
makedir("myfolder")
47.48 Fsize() function
The function return the file size in bytes.
Syntax:
FSize(File Handle) ---> Number (File Size in Bytes)
47.49 TrimAll() function
Remove all spaces and tabs characters from a string
Syntax:
TrimAll(cString) ---> cString # Without Spaces and Tabs
47.46. DirExists() function 395
8. Ring Documentation, Release 1.9
47.50 TrimLeft() function
Remove all spaces and tabs characters from the left side of a string
Syntax:
TrimLeft(cString) ---> cString # Without Spaces and Tabs from the left side
47.51 TrimRight() function
Remove all spaces and tabs characters from the right side of a string
Syntax:
TrimRight(cString) ---> cString # Without Spaces and Tabs from the right side
47.52 EpochTime() function
Return the Epoch Time
Syntax:
EpochTime(cDate,cTime) ---> nEpochTime
Example:
see EpochTime( Date(), Time() )
47.53 SystemCmd() Function
We can execute system commands using the SystemCmd() function that outputs to a variable
Syntax:
SystemCmd(cCommand)
Example:
cYou = SystemCmd("whoami") # User Name logged in is output a variable
cThem = SystemCmd("dir c:Users") # Directory List is output to a variable
47.54 ListAllFiles() Function
Using this function we can quickly do a process on a group of files in a folder and it’s sub folders.
Syntax:
ListAllFiles(cFolder,cExtension) ---> List of Files
Example:
47.50. TrimLeft() function 396
9. Ring Documentation, Release 1.9
aList = ListAllFiles("c:/ring/ringlibs","ring") # *.ring only
aList = sort(aList)
see aList
Example:
see listallfiles("b:/ring/ringlibs/weblib","") # All Files
47.55 SystemSilent() Function
We can execute system commands using the SystemSilent() function to avoid displaying the output!
Syntax:
SystemSilent(cCommand)
47.56 OSCreateOpenFolder() Function
Create folder then change the current folder to this new folder
Syntax:
OSCreateOpenFolder(cCommand)
47.57 OSCopyFolder() Function
Copy folder to the current folder
Parameters : The path to the parent folder and the folder name to copy
Syntax:
OSCopyFolder(cParentFolder,cFolderName)
Example
To copy the folder b:ringringlibsstdlib to the current folder
OSCopyFolder("b:ringringlibs","stdlib")
47.58 OSDeleteFolder() Function
Delete Folder in the current Directory
Syntax:
OSDeleteFolder(cFolderName)
47.55. SystemSilent() Function 397
10. Ring Documentation, Release 1.9
47.59 OSCopyFile() Function
Copy File to the current directory
Syntax:
OSCopyFile(cFileName)
47.60 OSDeleteFile() Function
Delete File
Syntax:
OSDeleteFile(cFileName)
47.61 OSRenameFile() Function
Rename File
Syntax:
OSRenameFile(cOldFileName,cNewFileName)
47.62 List2Code() Function
This function covert a Ring list during the runtime to Ring source code that we can save to source files.
The list may contains strings, numbers or sub lists.
Example:
load "stdlibcore.ring"
aList = 1:10
? list2Code(aList)
Output:
[
1,2,3,4,5,6,7,8,9,10
]
47.63 Str2ASCIIList()
Convert a string of bytes to a list of numbers where each item represent the ASCII code of one byte in the string.
Syntax:
Str2ASCIIList(String) ---> List of numbers
47.59. OSCopyFile() Function 398