[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Split an array into different variables?

Posted on 2003-11-14
10
Medium Priority
?
484 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
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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

830 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