SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ASP - Multidimentional Array

    I'm new to Multidimentional Array's and so I'm obviously not getting something

    On my website I want to divide a list of servers into pages - 5 a page. So I thought it would be best to use a multidimentional array. Array(x,y) - xage and y:servers

    SplitSearch = split("Server1,Server2,Server3,Server4,Server5,Server6,Server7,Server8,Server9,Server10,Server11,Server12,Server13")

    ArrayCounter = 0
    ArrayPage = 0

    dim PageArray

    NumberPerPage = 4

    for each x in SplitSearch
    If ArrayCounter = NumberPerPage then
    ArrayPage = ArrayPage + 1
    ArrayCounter = 0
    End if
    if x <> "" then
    ReDim PageArray(ArrayPage,ArrayCounter)
    PageArray(ArrayPage,ArrayCounter) = x
    ' response.write(PageArray(ArrayPage,ArrayCounter)&"<br/>")
    ArrayCounter = ArrayCounter + 1
    end if
    next

    response.write(PageArray(1,1)&"<br/>")
    Error:

    Microsoft VBScript runtime error '800a0009'

    Subscript out of range: '[number: 1]'
    The response.write(PageArray(ArrayPage,ArrayCounter)&"<br/>") works inside the loop. But it doesn't work outside of the loop.

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could try providing a character (delimiter) on which the string is to be split.

    Code:
    SplitSearch = split("Server1,Server2,Server3,Server4,Server5,Server6,Server7,Server8,Server9 ,Server10,Server11,Server12,Server13", ",")
    From the VBScript manual ...

    Split Function
    See Also
    Join Function
    Requirements
    Version 2
    Returns a zero-based, one-dimensional array containing a specified number of substrings.

    Split(expression[, delimiter[, count[, compare]]])

    Arguments
    expression
    Required. String expression containing substrings and delimiters. If expression is a zero-length string, Split returns an empty array, that is, an array with no elements and no data.
    delimiter
    Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
    count
    Optional. Number of substrings to be returned; -1 indicates that all substrings are returned.
    compare
    Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    You could try providing a character (delimiter) on which the string is to be split.

    Code:
    SplitSearch = split("Server1,Server2,Server3,Server4,Server5,Server6,Server7,Server8,Server9 ,Server10,Server11,Server12,Server13", ",")
    From the VBScript manual ...
    Oh yeah, it is actually on my asp page, but I just forgot to add it when I posted it on here (I needed to slightly modify it before adding it on here oops)

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The problem is that you are redimensioning the array each time, and at the end you have a dimension of (3,1). However you also need a dimension of (2,3) so this will throw an error. Here's some test code that works.

    Code ASP:
    <%
    Option Explicit
    Dim SplitSearch,ArrayCounter,ArrayPage,NumberPerPage,x,ArrayMax
    SplitSearch = split("Server1,Server2,Server3,Server4,Server5,Server6,Server7,Server8,Server9 ,Server10,Server11,Server12,Server13", ",")
    ArrayCounter = 0
    ArrayPage = 0
    NumberPerPage = 4
     
    dim PageArray ()
     
    for each x in SplitSearch
    	if Len (x) > 0 then
    		If ArrayCounter = NumberPerPage then
    			ArrayPage = ArrayPage + 1
    			ArrayMax = ArrayCounter
    			ArrayCounter = 0
    		End if
    		ArrayCounter = ArrayCounter + 1
    		response.write ArrayPage & "," & ArrayCounter & " = " & x & "<br/>"
    		End if
    next
     
    Redim PageArray (ArrayPage, ArrayMax)
    ArrayCounter = 0
    ArrayPage = 0 
     
    for each x in SplitSearch
    	if Len (x) > 0 then
    		If ArrayCounter = NumberPerPage then
    			ArrayPage = ArrayPage + 1
    			ArrayCounter = 0
    		End if	
    		PageArray (ArrayPage, ArrayCounter) = x
    		ArrayCounter = ArrayCounter + 1 
    	end if 
    next
     
    response.write(PageArray(1,1)&"<br/>")
    %>
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    357
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You might take a look at using the asp dictionary object rather than an array.

    http://www.w3schools.com/asp/asp_ref_dictionary.asp
    Doug G
    =====
    "If you ain't the lead dog, the view is always the same - Anon

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here's a version that removes the first loop.
    Code ASP:
    <%
    Option Explicit
    Dim SplitSearch,ArrayCounter,ArrayPage,NumberPerPage,x,ArrayMax,NumElems
    SplitSearch = split("Server1,Server2,Server3,Server4,Server5,Server6,Server7,Server8,Server9 ,Server10,Server11,Server12,Server13", ",")
     
    NumberPerPage = 4
    NumElems = Ubound(SplitSearch) + 1
    If Fix (NumElems / NumberPerPage) < 1 Then
    	ArrayPage = 0
    	ArrayMax = NumElems Mod NumberPerPage
    Else
    	ArrayPage = Fix (NumElems / NumberPerPage)
    	ArrayMax = NumberPerPage - 1
    End If
     
    Redim PageArray (ArrayPage,ArrayMax)
    ArrayCounter = 0
    ArrayPage = 0
     
    for each x in SplitSearch
        if Len (x) > 0 then
            If ArrayCounter = NumberPerPage then
                ArrayPage = ArrayPage + 1
                ArrayCounter = 0
            End if
            PageArray (ArrayPage, ArrayCounter) = x
            ArrayCounter = ArrayCounter + 1
        end if
    next
    Response.Write PageArray(1,1)
    %>
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    Here's a version that removes the first loop.
    Code ASP:
    <%
    Option Explicit
    Dim SplitSearch,ArrayCounter,ArrayPage,NumberPerPage,x,ArrayMax,NumElems
    SplitSearch = split("Server1,Server2,Server3,Server4,Server5,Server6,Server7,Server8,Server9 ,Server10,Server11,Server12,Server13", ",")
     
    NumberPerPage = 4
    NumElems = Ubound(SplitSearch) + 1
    If Fix (NumElems / NumberPerPage) < 1 Then
    	ArrayPage = 0
    	ArrayMax = NumElems Mod NumberPerPage
    Else
    	ArrayPage = Fix (NumElems / NumberPerPage)
    	ArrayMax = NumberPerPage - 1
    End If
     
    Redim PageArray (ArrayPage,ArrayMax)
    ArrayCounter = 0
    ArrayPage = 0
     
    for each x in SplitSearch
        if Len (x) > 0 then
            If ArrayCounter = NumberPerPage then
                ArrayPage = ArrayPage + 1
                ArrayCounter = 0
            End if
            PageArray (ArrayPage, ArrayCounter) = x
            ArrayCounter = ArrayCounter + 1
        end if
    next
    Response.Write PageArray(1,1)
    %>
    I can't believe it - you're an absolute magician! Thanks so much - hopefully the hair that I had pulled out can now grow back!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •