Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to ignore real commas when converting to an array?

    Hi,

    I am using ASP and a SQL database. Here is something I would think would be a more common issue.

    I have an HTML menu list ( "<option>" tag) that pulls it's values from a SQL database. For example lets say these are some of the values that pulled.

    Fielding USA
    Bigger Buys
    Nation Source, Inc.
    Sterners


    So if the user where to choose all of these and submit the form, the string would look like this:


    mystring = Fielding USA, Bigger Buys, Nation Source, Inc. Sterners

    I then convert this to an array by looking at the commas like this
    Code:
    <%
    
    thisArray=Split(mystring ,",")  '' Turns the comma seperated string into an array
    
    %>
    So as you can see when I convert this to an array the comma after source should be ignored and makes it look like this:

    Fielding USA
    Bigger Buys
    Nation Source
    Inc.
    Sterners

    I suppose this is similar to the single quote issue that I see on the sticky, but if I don't have to I don't want to eliminate commas from my data.

    Does anyone know a way around this? I was thinking maybe there is a way to tell the option tag to to not use commas to seperate the values? I would rather use something like "|".
    Thanks for the help
    Last edited by sherbert99; 03-13-2006 at 10:10 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    When receiving the posted form, you should iterate over the selected options to store them in an array instead of just concatenating them to a comma-separated string and later splitting it up into an array
    I am the luckiest man in the world

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    (why cant i edit my own post?)

    Like:
    Dim size = Request.Form("theselectfieldname).Count

    For i = 1 To size
    'add the ith element of the formcollection to your array
    yourarray(i) = request.Form("theselectfieldname")(i)
    Next
    I am the luckiest man in the world

  • #4
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would agree with the above method.
    Another option is to store the comma as an ASCII value.
    http://www.w3schools.com/tags/ref_ascii.asp

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great I like that method. Thanks.

    Any idea though how to declare the array because it gives me an error if I don't declare it. If I hard code it with a number like "3" it works. But the number of items in the array will vary. I tried setting it to the size like this:

    Code:
    Dim thisArray(size)
    But it causes an error. "Expected integer constant"

    Here's the code

    Code:
    Dim size 
    size = Request.Form("theselectfieldname").Count
    
    Dim thisArray(????)' How to make this dynamic because it wont always be the same
    
    For i = 1 To size
    'add the ith element of the formcollection to your array
    thisArray(i) = request.Form("theselectfieldname")(i)
    
    Next

    Thanks again
    Last edited by sherbert99; 03-14-2006 at 11:52 PM.

  • #6
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,072
    Thanks
    4
    Thanked 8 Times in 8 Posts
    In VBScript You must first declare the array with a numerical value. Then you can use the ReDim keyword to declare the array to a different size.

    As an example

    Dim myArray(3)

    Dim x : x = 'assign a numerical value to x
    ReDim myArray(x)

  • #7
    New Coder
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I could have sworn I tried the "Redim" and it didn't work, but it works now. Thanks


  •  

    Posting Permissions

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