Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 17th, 2008, 06:05 PM
Authorized User
Points: 292, Level: 5
Points: 292, Level: 5 Points: 292, Level: 5 Points: 292, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: , , .
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.
          :)
Reply With Quote
  #2 (permalink)  
Old November 17th, 2008, 06:17 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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.
Reply With Quote
  #3 (permalink)  
Old November 17th, 2008, 08:22 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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
Reply With Quote
  #4 (permalink)  
Old November 18th, 2008, 01:39 PM
Authorized User
Points: 292, Level: 5
Points: 292, Level: 5 Points: 292, Level: 5 Points: 292, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: , , .
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!!!
Reply With Quote
  #5 (permalink)  
Old November 18th, 2008, 01:42 PM
Authorized User
Points: 292, Level: 5
Points: 292, Level: 5 Points: 292, Level: 5 Points: 292, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: , , .
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!

Reply With Quote
  #6 (permalink)  
Old November 18th, 2008, 05:08 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:12 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.