Tokenize a string: Difference between revisions
Content deleted Content added
added MMIX |
added BASIC, PowerBASIC, Visual Basic |
||
Line 117: | Line 117: | ||
which "tokenize" each line of input and this is achieved by using "," as field separator |
which "tokenize" each line of input and this is achieved by using "," as field separator |
||
=={{header|BASIC}}== |
|||
{{works with|QBasic}} |
|||
<lang qbasic>DIM parseMe AS STRING |
|||
parseMe = "Hello,How,Are,You,Today" |
|||
DIM tmpLng1 AS INTEGER, tmpLng2 AS INTEGER, parsedCount AS INTEGER |
|||
tmpLng2 = 1 |
|||
parsedCount = -1 |
|||
'count number of tokens |
|||
DO |
|||
tmpLng1 = INSTR(tmpLng2, parseMe, ",") |
|||
IF tmpLng1 THEN |
|||
parsedCount = parsedCount + 1 |
|||
tmpLng2 = tmpLng1 + 1 |
|||
ELSE |
|||
IF tmpLng2 < (LEN(parseMe) + 1) THEN parsedCount = parsedCount + 1 |
|||
EXIT DO |
|||
END IF |
|||
LOOP |
|||
IF parsedCount > -1 THEN |
|||
REDIM parsed(parsedCount) AS STRING |
|||
tmpLng2 = 1 |
|||
parsedCount = -1 |
|||
'parse |
|||
DO |
|||
tmpLng1 = INSTR(tmpLng2, parseMe, ",") |
|||
IF tmpLng1 THEN |
|||
parsedCount = parsedCount + 1 |
|||
parsed(parsedCount) = MID$(parseMe, tmpLng2, tmpLng1 - tmpLng2) |
|||
tmpLng2 = tmpLng1 + 1 |
|||
ELSE |
|||
IF tmpLng2 < (LEN(parseMe) + 1) THEN |
|||
parsedCount = parsedCount + 1 |
|||
parsed(parsedCount) = MID$(parseMe, tmpLng2) |
|||
END IF |
|||
EXIT DO |
|||
END IF |
|||
LOOP |
|||
PRINT parsed(0); |
|||
FOR L0 = 1 TO parsedCount |
|||
PRINT "."; parsed(L0); |
|||
NEXT |
|||
END IF</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 714: | Line 763: | ||
so the conversion to vector is purely to satisfy task formulation. |
so the conversion to vector is purely to satisfy task formulation. |
||
=={{header|PowerBASIC}}== |
|||
PowerBASIC has a few keywords that make parsing strings trivial: <code>PARSE</code>, <code>PARSE$</code>, and <code>PARSECOUNT</code>. (<code>PARSE$</code>, not shown here, is for extracting tokens one at a time, while <code>PARSE</code> extracts all tokens at once into an array. <code>PARSECOUNT</code> returns the number of tokens found.) |
|||
<lang powerbasic>FUNCTION PBMAIN () AS LONG |
|||
DIM parseMe AS STRING |
|||
parseMe = "Hello,How,Are,You,Today" |
|||
REDIM parsed(PARSECOUNT(parseMe) - 1) AS STRING |
|||
PARSE parseMe, parsed() 'comma is default delimiter |
|||
DIM L0 AS LONG, outP AS STRING |
|||
outP = parsed(0) |
|||
FOR L0 = 1 TO UBOUND(parsed) 'could reuse parsecount instead of ubound |
|||
outP = outP & "." & parsed(L0) |
|||
NEXT |
|||
MSGBOX outP |
|||
END FUNCTION</lang> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 851: | Line 920: | ||
<pre>'Hello.How.Are.You.Today'</pre> |
<pre>'Hello.How.Are.You.Today'</pre> |
||
=={{header|Visual Basic}}== |
|||
{{trans|PowerBASIC}} |
|||
Unlike PowerBASIC, there is no need to know beforehand how many tokens are in the string -- <code>Split</code> automagically builds the array for you. |
|||
<lang vb>Sub Main() |
|||
Dim parseMe As String, parsed As Variant |
|||
parseMe = "Hello,How,Are,You,Today" |
|||
parsed = Split(parseMe, ",") |
|||
Dim L0 As Long, outP As String |
|||
outP = parsed(0) |
|||
For L0 = 1 To UBound(parsed) |
|||
outP = outP & "." & parsed(L0) |
|||
Next |
|||
MsgBox outP |
|||
End Sub</lang> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |