Solved

Split an array into different variables?

Posted on 2003-11-14
10
476 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

11 Experts available now in Live!

Get 1:1 Help Now