Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2008, 06:05 PM
Authorized User
 
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing from a text string

I'm trying to do something that I have a feeling should be pretty simple. I need to pull out variables from a text string.

The string is:

client=1|invno=123|filter=yes|food=calamari

The variables themselves are dynamic. I'm trying to use the |(pipe) as a separator character.

Any ideas are truly appreciated.
          :)
 
Old November 17th, 2008, 06:17 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

So do you mean that, in the end, you want to have VBScript variables with the same names set to those values??

There are several ways to do this, but as ugly as the following looks, it's probably the most efficient:
Code:
<%
...
str = "client=1|invno=123|filter=yes|food=calamari"

pairs = Split( str, "|" )
For p = 0 To UBound(pairs)
    vv = Split( pairs(p), "=" )
    vname = vv(0)
    vvalue = vv(1)

    ' could also check for boolean and datetime values and could special case integers
    If IsNumeric(vvalue) Then vvalue = CDbl(vvalue) 

    ' then assign to correct variable
    Select Case vname
        Case "client" : client = vvalue
        Case "invno"  : invno  = vvalue
        Case "filter" : filter = vvalue
        Case "food"   : food   = vvalue
        Case Else : Response.Write "Unrecognized variable name: " & vname & ""
    End Select
Next
...
%>
****************

Note that the code
    filter = vvalue
will cause an error, because filter is a keyword. You can choose another variable name.
 
Old November 17th, 2008, 08:22 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Just for the tasty flat fish*, here's another way:
Code:
<%
str = "client=1|invno=123|xfilter=yes|food=calamari"

Set re = New RegExp
re.Pattern = "\=([^\|]*)\|"
re.Global = True

Execute re.Replace( str & "|Rem", "=""$1"" : " )
%>

client: <%=client%><br>
invno: <%=invno%><br>
xfilter: <%=xfilter%><br>
food: <%=food%>
Has the disadvantage that all your variables will be string values (that is, will do
    client="1"
instead of
    client=1
) but...

I had to change your filter to another name; filter is a keyword in VBScript, so you can't assign to it.

**********************************

* halibut
 
Old November 18th, 2008, 01:39 PM
Authorized User
 
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Old...the 2nd example you showed would be great. How would I go about changing the text field to a numeric field after the fact? The xfilter field would need to be multiplied in a couple more programming lines.

Thanks!!!
 
Old November 18th, 2008, 01:42 PM
Authorized User
 
Join Date: Dec 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What I'm actually trying to do is stack up a bunch of variables, send the whole string as a "pass-through" value on someone else's programming, then get it back and break it up again. *sigh* I'm only allowed one passthrough string. So, all the variable I listed would have a coordinating value already, I just need to send it to the next routine.

Thanks!

 
Old November 18th, 2008, 05:08 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

xfilter = CDBL(xfilter)

Presto, it's now a numeric value.

&&&&&&&&&&&&&&

I'm afraid I have no idea what you are really trying to do. I dunno what a "pass-through" string is, unless you mean a query string.

Oh! Wait! You must be talking about something like a payment processing system. So you mean the "pass-through" is (a) completely in your control and (b) it will be completely UNALTERED by the other system.

If that's so, then I recommend my first solution as superior. It has the huge advantage that you can, indeed, be sure you are getting the right value type (string, integer, double, etc.) on the final page. And it's also *much* faster and more efficient than the Execute scheme, which is really a CPU hog.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing of String jmss66 VB How-To 9 May 1st, 2008 12:47 AM
Parsing a String? MBowen SQL Server 2000 9 December 1st, 2006 01:17 PM
Parsing a text string medix_911 Access 8 November 21st, 2006 01:37 PM
Parsing Large Delimited Text String Coop64 SQL Language 1 February 16th, 2004 03:19 PM
Parsing a string of numbers tp194 Classic ASP Databases 1 January 5th, 2004 10:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.