ASP > Arrays articles:
• Dumping the contents of a 2-dimensional array
Read
• Sorting a multi-dimensional array in ASP VBScript
Read
• Splitting delimited text into a multi-dimensional array
|
Return to index of articles
Splitting delimited text into a multi-dimensional array
Category: ASP
Category: Arrays
VBScript provides the SPLIT function to split text into an array, assuming the text is delimited by something like commas. For example the SPLIT function can split
blue,red,green,yellow
into an array where each colour is a separate element of the array. But what if you have a multi-line comma-delimited file or field such as data exported from Access, for example:
1/1/2007,james,smith,123 main st.,anytown,st,00001
2/2/2007,bill,black,234 centre st.,anyville,st,12345
and so on...
Ideally in this situation you would like to create a multi-dimensional array where each row represents a line of text and each column represents a data element. This can be done with the following function. Just pass the source text, row separator and column separator, e.g.
SplitMulti( strTXT, ",", vbcrlf )
or
SplitMulti( strTXT, "|", ";" )
function SplitMulti( s , r , c )
s = trim(s)
' eliminate final delimeter which creates blank row
do while right(s,1)=r
s = left(s, len(s)-1)
loop
dim a,t,u,y,x
t = split( s , r )
if( UBound(t) > -1 )then
u = split( t(0) , c )
redim a( ubound(u) , ubound(t) )
for y = 0 to ubound(t) ''rows
u = split( t(y) , c )
for x = 0 to ubound(u) ''cols
a( x,y ) = u(x)
next
next
SplitMulti= a
else
SplitMulti= null
end if
end function
4/2/2008
|