Solved

Split an array into different variables?

Posted on 2003-11-14
10
480 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
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!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 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