Solved

Split an array into different variables?

Posted on 2003-11-14
10
475 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 information …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now