Solved

Split an array into different variables?

Posted on 2003-11-14
10
479 Views
Last Modified: 2010-05-18
Hi guys
Is there any way in asp where we can take an array and assign its elements individually to new variables ?
Like Name/Bob/Age/28/City/NY
would be something like
Strname="Bob" : IntAge=28 : StrCity="NY"

Thanks

0
Comment
Question by:Ageil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9749727
Ageil:

> Is there any way in asp where we can take an array and assign its elements
> individually to new variables ?
> Like Name/Bob/Age/28/City/NY

Sort of.  You can do this:

     Function SplitArray( strData )
          Dim arr
          Dim dict
          Dim nCounter

          Set dict = CreateObject("Scripting.Dictionary")
          arr = Split( strData )
          For nCounter = 0 To UBound(arr) Step 2
               dict(arr(nCounter)) = arr(nCounter+1)
          Next

          Set SplitArray = dict
     End Function

Then you can do this:

     Dim str
     Dim objValues

     str = "Name/Bob/Age/28/City/NY"
     Set objValues = SplitArray( str )

     Response.Write( "Name = " & objValues("Name") )

To Print Out:
     Name = Bob

Will that work for you?


Hope That Helps,
Dex*
0
 
LVL 28

Expert Comment

by:sybe
ID: 9749928
sString  = "Name/Bob/Age/28/City/NY"
aArray = Slit(sString,"/")
For i = 0 To Ubound(aArray) Step 2
     Execute(aArray(i) & " = " & Array(i+1))
Next

Response.write Name & "<br>"
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9749964
sybe:

Will that still work if you use "OPTION EXPLICIT"?  Oh, and thanks for reminding me that this line:
     arr = Split( strData )

Should have been:
     arr = Split( strData, "/" )

Dex*
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ageil
ID: 9750011
Dexstar

I keep getting this error when i tried it

Microsoft VBScript runtime error '800a0009'

Subscript out of range: '[number: 1]'

0
 
LVL 19

Accepted Solution

by:
Dexstar earned 100 total points
ID: 9750031
Ageil:

See my previous post.  There is a small mistake in my original code.  SplitArray should look like this:

     Function SplitArray( strData )
          Dim arr
          Dim dict
          Dim nCounter

          Set dict = CreateObject("Scripting.Dictionary")
          arr = Split( strData, "/" )
          For nCounter = 0 To UBound(arr) Step 2
               dict(arr(nCounter)) = arr(nCounter+1)
          Next

          Set SplitArray = dict
     End Function

Enjoy,
Dex*
0
 

Author Comment

by:Ageil
ID: 9750045
Sybe
when i used your example i got a type mismatch

Microsoft VBScript runtime error '800a000d'

Type mismatch

ofcourse thats after correcting the split/slit

0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9750079
Ageil:

To get Sybe's solution working properly, you should change it to this:

sString  = "Name/Bob/Age/28/City/NY"
aArray = Split(sString,"/")
For i = 0 To Ubound(aArray) Step 2
      If ( IsNumeric(aArray(i+1)) ) Then
            Execute(aArray(i) & " = " & aArray(i+1))
      Else
            Execute(aArray(i) & " = """ & aArray(i+1) & """")
      End If
Next
0
 

Author Comment

by:Ageil
ID: 9750119
Thank you very much guys

0
 

Author Comment

by:Ageil
ID: 9750133
is there any place where i can read about the dictionary object?

0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9750161
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Summernote and form validation 10 232
ASP Button to clear text 4 69
VBScript on Html 15 59
Filktering Alphabetically 8 44
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question