ASP (in VBScript) array problem - type mismatch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yandhi
    New Member
    • Oct 2008
    • 7

    ASP (in VBScript) array problem - type mismatch

    Greetings,
    This is my first post here but I've been reading here a while and have developed a bit of a problem I thought I'd seek some advice on.

    As the subject suggests, I'm building a page with ASP and using VBScript as my language. I'm relatively new to programming in general so forgive me if I'm way off base here somewhere.

    What I'm doing is reading file names from a directory on my server and storing them as one long string in a variable. The filenames are actually a relative path like "/folder/filename.jpg." These are being brought in with the FileSystemObjec t and loaded in to the variable with a loop.
    Code:
    	for each item in folder.Files
    		url = MapURL(item.path)
    		
    		' store the file names
    		if fileList = NULL Then
    			fileList = url & ","
    		else
    			fileList = fileList & url & ","
    		end if
    	next
    Then, I try to write the contents out...

    Code:
    	response.write(fileList)
    	response.write("<br><br>")
    	
    	newList = split(fileList,",")
    	
    	for each i in newList
    		response.write(newList(i) & "<br>") '<--- this line throws the error
    	next
    Now, in researching this up to this point (even on these very forums), I learned that when you split a variable like I've done to fileList, newList becomes an array of everything separated by a "," in fileList.

    But, when I try to write the values saved in the array I get an error "Type mismatch: '/testfolder/testimg.jpg'"

    I've tried quite a bit but obviously haven't been able to get it working. What am I missing here?

    Not that I think it matters for this particular issue, but I'm running IIS on XP Pro.

    Thanks in advance!

    Edit --> response.write( fileList) prints all the relative paths just fine just like the code looks like it should.

    /folder/testimage1.jpg,/folder/testimage2.jpg,/folder/testimage3.jpg,/folder/testimage4.jpg and on and on.
  • yandhi
    New Member
    • Oct 2008
    • 7

    #2
    Actually... nevermind. I took a break and when I came back I realized my problem was a fundamental misunderstandin g of looping through an array in VBScript.

    Unlike in C++ where you would say something like...

    Code:
    for (int i = 0; i < n; i++) {
         cout << myArray[i] << endl;
    }
    I was following that same style logic with this and that failed to accomplish the goal.

    Instead, I changed my loop to...

    Code:
    	for each i in newList
    		response.write(i & "<br>")
    	next
    Anyway, just thought I'd put my solution up in the public as well in the event other beginners were running in to the same type of problem.

    Thanks for your consideration. I'm sure I'll be back.

    - John

    Comment

    • DrBunchman
      Recognized Expert Contributor
      • Jan 2008
      • 979

      #3
      Hi John,

      I'm glad you fixed your problem and thanks for posting the solution.

      Oh and welcome to Bytes!

      Dr B

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        Originally posted by yandhi
        Actually... nevermind. I took a break and when I came back I realized my problem was a fundamental misunderstandin g of looping through an array in VBScript.

        Unlike in C++ where you would say something like...

        Code:
        for (int i = 0; i < n; i++) {
             cout << myArray[i] << endl;
        }
        I was following that same style logic with this and that failed to accomplish the goal.

        Instead, I changed my loop to...

        Code:
        	for each i in newList
        		response.write(i & "<br>")
        	next
        Anyway, just thought I'd put my solution up in the public as well in the event other beginners were running in to the same type of problem.

        Thanks for your consideration. I'm sure I'll be back.

        - John
        Right, because if you say "for each i in newList" you make i be each of the elements, not the index of the elements. To do a similar thing to what you did in C++ you would say "for i = 0 to ubound(newList) ". Anyway, glad you found your solution.

        Jared

        Comment

        • yandhi
          New Member
          • Oct 2008
          • 7

          #5
          Ahh awesome, thanks Jared. Much appreciated. I'll definitely keep that in mind.

          Comment

          Working...