Forum Moderators: open

Message Too Old, No Replies

Reading CSV into array with ASP

         

ukgimp

11:32 am on Jul 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have looked around the web for the rough solution to this but to no avail. ASP is not really my bag.

I have a set of CSV from a form in roughly the following format:

Name1,email1,title1
Name2,email2,title2
Name3,email3,title3

….n

I would like to read these into variables, rather like and array in php then loop through them and send a mail to each. Any pointers would be greatly appreciated, I ripped some hair out yesterday. :)

Cheers

Dreamquick

12:02 pm on Jul 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a go with this - it'll go through your three column comma delimited file line-by-line and splits up the data into an array.

From there it should be simple enough to remove my demo code and add your email component code into the loop...

<%
Option Explicit

'Code to display data from the current text-based logfile
Dim objFSO, oInStream, sLine, sSeg

'Define the constants used by the FSO
Const ForReading = 1

'Create an instance of the FSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Check the file exists
If objFSO.FileExists( Server.MapPath( "stuff.txt" ) ) Then

'Open a file for reading
Set oInStream = objFSO.OpenTextFile( Server.MapPath( "stuff.txt" ), ForReading, False )

Do Until oInStream.AtEndOfStream

sLine = oInStream.ReadLine
sSeg = Split( sLine, "," )

Response.Write "<b>A</b>: " & sSeg(0)
Response.Write "<b>B</b>: " & sSeg(1)
Response.Write "<b>C</b>: " & sSeg(2)
Response.Write "<br>"

Loop

oInStream.Close
Set oInStream = Nothing

Else

Response.Write "File not found!"

End If
Set objFSO = Nothing
%>

- Tony

ukgimp

3:14 pm on Jul 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks BF

Still having difficulties, I tried variations of your code but to no avail. I have managed to get a little bit closer to my aim by getting all the values from the string which are separated into an array and print them out to the page.

I still cant figure how to get them into 3 different arrays.

Here is a snippet of how far I have got.

Cheers.

dim strData, strOneWord, array
strData = Request.Form("data_input")
strData = replace(strData,vbCRLF,",")

array = split(strData , ",", -1 , 1)

For Each strOneWord in array
response.write(strOneWord)
response.write("<br>")
Next

ukgimp

11:36 am on Jul 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Worked on this for a while and got it to work. In this case I have not added the it to the multidemsional array, rather each value is added into variable and the work is done,. In this case the variable is printed out. Instead of assiging it to a variable you could loop through and add to a 3 different arrays. Not sure which is quicker, but is work. If you wanted to create a 4 dimentional array you would simply adjust the iterations divide by value.

strData = Request.Form("data_input")
'replace new line with a comma
strData = replace(strData,vbCRLF,",")
arrayData = split(strData , ",", -1 , 1)

count = UBound(arrayData)
iterations = ((count + 1) / 3)

for i=0 to (iterations - 1)

email = arrayData((i * 3))
name = arrayData(((i * 3) +1))
url = arrayData(((i * 3)+2))
'write to page to prove it works or do your function
response.write(email) & "&nbsp" & (name) & "&nbsp" & (url) & "<hr>"

next

Dreamquick

7:15 pm on Jul 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for the lack of feedback, not had as much free time as I'd like recently!

Multi-dimensional arrays are available if you need to use them. You can build a static one like this;

Dim sArray(0 to 10, 0 to 10)

Or you can make a dynamic array which can be resized (well within limits, you can change the last dimension but no others).

Very quick example of expanding an array;

'Create & initialise
Dim sArray()
ReDim sArray(0 to 3,0)

sArray(0 to 3,0) = ""

...

Do Until Data.EOF

'Note the preserve, this retains data after the resize
If sArray(0,UBound( sArray, 2 )) <> "" Then
'No more empty variables, expand the array
ReDim Preserve sArray(0 to 3, LBound( sArray, 2 ) To UBound( sArray, 2 )+1)
End If

sArray(0,UBound( sArray, 2 )) = Data("Field1").Value
sArray(1,UBound( sArray, 2 )) = Data("Field2").Value
sArray(2,UBound( sArray, 2 )) = Data("Field3").Value
sArray(3,UBound( sArray, 2 )) = Data("Field4").Value

Data.NextRec
Loop

...

- Tony

ukgimp

10:13 am on Jul 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



response.write(email) & "&nbsp" & (name) & "&nbsp" & (url) & "<hr>"

Weirdness with this line. It workd fine but whenb I try to construct the body of my email it does not work, it just picks the first one from the array.

Is there a problem with variables inside functions like in php.

ukgimp

1:40 pm on Jul 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Solution:

Go to lunch after doing around 100 variations of the code.

Realise that you were concatenating some varaibles and they were beiung carried through for each loop

Find out about clearing variables with a clear() command so each loop starts afresh.

Cheers

JuniorHarris

6:20 pm on Jul 9, 2003 (gmt 0)

10+ Year Member



Double check the context of your variant (local/global).