Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Monday, June 9, 2008

SilkTest - Make sublist from a list

I have written a function to get sublist from a list of items. I have done for STRING type. You can try for ANYTYPE data type.Its usage is similar to SubString. See the following code snippet.

Silktest code sample: GetSubList

[+] public LIST OF STRING GetSubList(LIST OF STRING lsInput, STRING sFirst optional, STRING sLast optional, Boolean bMatch optional) [ ] //To get String list between sFirst and sLast [ ] //If sFirst is NULL or "", SubList will be taken from first item of list [ ] //If sSecond is Null or "", SubList will be taken upto last item of list [ ] //bMatch - FALSE means exact match, TRUE means Partial Match [ ] Integer iCount, iItem [ ] Boolean bFirst = FALSE [ ] Boolean bLast = FALSE [ ] LIST OF STRING lsReturn = {} [ ] [-] if (IsNull(bMatch)) [ ] bMatch = FALSE [-] if (IsNull(sFirst) || (sFirst == "")) [ ] bFirst = TRUE [ ] [ ] iCount = ListCount(lsInput) [-] for iItem=1 to iCount [-] if !(bFirst) [-] if !(bMatch) [-] if (sFirst == lsInput[iItem]) [ ] bFirst = TRUE [ ] ListAppend(lsReturn,lsInput[iItem]) [-] else [-] if (Len(sFirst) <= Len(lsInput[iItem])) [-] if (MatchStr("*{sFirst}*",lsInput[iItem])) [ ] bFirst = TRUE [ ] ListAppend(lsReturn,lsInput[iItem]) [+] else [-] if !(bLast) [ ] ListAppend(lsReturn,lsInput[iItem]) [-] if !(IsNull(sLast)) [-] if !(bMatch) [-] if (sLast == lsInput[iItem]) [ ] bLast = TRUE [-] else [-] if (MatchStr("*{sLast}*",lsInput[iItem])) [ ] bLast = TRUE [-] else [ ] break //Exit from for loop [ ] [ ] return lsReturn [ ]

Thursday, April 17, 2008

Convert Number to Word

In my first job, I needed to have a function to verify the number to string conversion. The number should be converted to the word. For example, if user enters 8908 and function returns the output as Eight Thousand Nine Hundred and Eight only. I thought that it is very simple. After starting to write it, I faced few issues. Thats why I created four functions. I have given the code below. The current code converts up to crores. This code snippet can be used in Visual Test or Visual Basic. Few changes needs to be done to use in VB Script.

Code: Returns the word to the given number



'---------------------------
' Method : NumberToWord
' Author : T.Palani Selvam
' Purpose : Returns the word to the given number.
' Parameters: sGivenNum - String, contains to be converted number.
'
' Returns : Returns String of corresponding number.
' Caller :
' Calls : - Nil -
'----------------------------
Function NumberToWord(sGivenNum As String) As String
Dim lBeforeDecimal As Long, lAfterDecimal As Long
Dim iDecimalPos As Integer, iIndex As Integer
Dim sBeforeDecimalWord As String, sAfterDecimalWord As String
Dim sResult As String, sChar As String

'sResult = Null
sGivenNum = Trim(Str(Val(sGivenNum)))
If Val(sGivenNum) = 0 Then
NumberToWord = "Zero"
Exit Function
End If
sAfterDecimalWord = ""
sBeforeDecimalWord = ""
iDecimalPos = InStr(sGivenNum, ".")
If iDecimalPos = 0 Then
lBeforeDecimal = Val(sGivenNum)
Else
'sGivenNum = Format(sGivenNum, "#####.00")
lBeforeDecimal = Val(Left(sGivenNum, iDecimalPos - 1))
lAfterDecimal = Val(Mid(sGivenNum, iDecimalPos + 1))
If (lAfterDecimal <> 0) Then
'sAfterDecimalWord = " Paise " + PrimaryConversion(lAfterDecimal)
sAfterDecimalWord = " Point" '+ PrimaryConversion(lAfterDecimal)
For iIndex = 1 To Len(trim(str(lAfterDecimal)))
sChar = Mid$(trim(str(lAfterDecimal)), iIndex, 1)
sAfterDecimalWord = sAfterDecimalWord + " " + PrimaryConversion(val(sChar))
Next iIndex
End IF
If (lBeforeDecimal = 0) Then
NumberToWord = sAfterDecimalWord
Exit Function
End IF
End If

sBeforeDecimalWord = SplitType(10000000, " Crore ", lBeforeDecimal)
sBeforeDecimalWord = sBeforeDecimalWord + SplitType(100000, " Lakh ", lBeforeDecimal)
sBeforeDecimalWord = sBeforeDecimalWord + SplitType(1000, " Thousand ", lBeforeDecimal)
sBeforeDecimalWord = sBeforeDecimalWord + SplitType(100, " Hundred ", lBeforeDecimal)
If sBeforeDecimalWord = "" Then
sBeforeDecimalWord = PrimaryConversion(lBeforeDecimal)
Else
sBeforeDecimalWord = sBeforeDecimalWord + "and " + PrimaryConversion(lBeforeDecimal)
End If
sResult = sBeforeDecimalWord

If sAfterDecimalWord = "" Then
sResult = sBeforeDecimalWord
Else
'sResult = sBeforeDecimalWord + " and" + sAfterDecimalWord
sResult = sBeforeDecimalWord + sAfterDecimalWord
End If

NumberToWord = sResult '+ " Only"
End Function


Code: Set the word according to level


'-------------------------
' Method : SplitType
' Author : T.Palani Selvam
' Purpose : Splits a level to next level.
' Parameters: lNumType - Long, contains like 100, 1000
' sWord - String, contains before conversioned word.
' Returns : Returns String of corresponding number.
'----------------------------
Function SplitType(lNumType As Long, sWord As String,lBeforeDecimal As long ) As String
'Dim lBeforeDecimal As Long
Dim lNumberType As Long

SplitType = ""
lNumberType = lBeforeDecimal \ lNumType
If lNumberType > 0 Then
lBeforeDecimal = lBeforeDecimal - lNumberType * lNumType
SplitType = PrimaryConversion(lNumberType) + sWord
End If
End Function


Code: Parse a string to the given condition


'--------------------------
' Method : GetParsedString
' Author : T.Palani Selvam
' Purpose : Parse a string to the given condition
' Parameters: sData - String, contains full string..
' sSeparate - String, contains separation string
' iOccur - String, contains position of the Split
' Returns : Returns String, Null or other values.
'--------------------------
Function GetParsedString(sData As String, sSeparate As String, iOccur As Integer) As String
Dim iPosition As Integer, iIndex As Integer
Dim sTemp As String

GetParsedString = Null
sTemp = sData

For iIndex =1 To (iOccur -1) Step 1
iPosition = Instr(sTemp,sSeparate)
If (iPosition > 0) Then
sTemp=Mid$(sTemp,len(sSeparate) + iPosition)
Else
Print(sSeparate + " is not occured")
Exit Function
End If
' Print "Original Symbol: " + sTemp, sSeparate
Next iIndex
iPosition = Instr(sTemp,sSeparate)
If (iPosition > 0) Then
sTemp=Left$(sTemp,iPosition - 1)
End If
GetParsedString = sTemp
End Function


Code: Converts primary number to word


'----------------------
' Method : PrimaryConversion
' Author : T.Palani Selvam
' Purpose : It converts primary number to string.
' Parameters: lNumber - Long, contains number
'
' Returns : Returns String of corresponding number.
'-------------------------
Function PrimaryConversion(lNumber As Long) As String
PrimaryConversion = ""
Select Case lNumber
Case 0
Case 1
PrimaryConversion = "One"
Case 2
PrimaryConversion = "Two"
Case 3
PrimaryConversion = "Three"
Case 4
PrimaryConversion = "Four"
Case 5
PrimaryConversion = "Five"
Case 6
PrimaryConversion = "Six"
Case 7
PrimaryConversion = "Seven"
Case 8
PrimaryConversion = "Eight"
Case 9
PrimaryConversion = "Nine"
Case 10
PrimaryConversion = "Ten"
Case 11
PrimaryConversion = "Eleven"
Case 12
PrimaryConversion = "Twelve"
Case 13
PrimaryConversion = "Thirteen"
Case 14
PrimaryConversion = "Fourteen"
Case 15
PrimaryConversion = "Fifteen"
Case 16
PrimaryConversion = "Sixteen"
Case 17
PrimaryConversion = "Seventeen"
Case 18
PrimaryConversion = "Eighteen"
Case 19
PrimaryConversion = "Nineteen"
Case 20
PrimaryConversion = "Twenty"
Case 30
PrimaryConversion = "Thirty"
Case 40
PrimaryConversion = "Forty"
Case 50
PrimaryConversion = "Fifty"
Case 60
PrimaryConversion = "Sixty"
Case 70
PrimaryConversion = "Seventy"
Case 80
PrimaryConversion = "Eighty"
Case 90
PrimaryConversion = "Ninety"
Case Else
PrimaryConversion = PrimaryConversion(lNumber - (lNumber Mod 10)) + " " + PrimaryConversion(lNumber Mod 10)
End Select
End Function

Wednesday, March 12, 2008

4test - Search and Replace

One project required to replace certain tags at every build. I written this function SearchAndReplaceInFile. This code snippet to search and replace strings in a file.

Code:



[+] public void SearchAndReplaceInFile (STRING sFileInput, STRING sFileOutput, STRING sSearch, STRING sReplace )
HFILE hInFile, hOutFile
STRING sTemp, sLine, sValue
INTEGER iPos1, iPos2

hInFile = FileOpen (sFileInput,FM_READ)
hOutFile = FileOpen (sFileOutput, FM_WRITE)

[-] while (FileReadLine (hInFile, sLine))
sTemp = sLine
[-] if !(Trim(sSearch) == Trim (sReplace))
ReadLine:
iPos1 = StrPos (sSearch,sTemp)
//Print ("Position {sTemp} - {sSearch} : {iPos1}")
[-] if (iPos1 > 0)
sValue = StrTran (sTemp, sSearch, sReplace)
//Print ("got value: {sValue}")
sTemp = sValue
goto ReadLine
FileWriteLine(hOutFile,sTemp)
FileClose (hInFile)
FileClose (hOutFile)