Solved

REFind variables

Posted on 2008-06-25
12
266 Views
Last Modified: 2013-12-24
I have a comma delimited set of variables. For example (rone, gtwo, bthree). The variables are dynamic so the lengths can vary. The first value, rone is r=firstname and one=last name. How can I use REFIND to strip out this values?
0
Comment
Question by:usky1
  • 6
  • 6
12 Comments
 
LVL 39

Expert Comment

by:gdemaria
ID: 21868004
Could you please clarify ..

 you have a list of strings "rone, gtwo, bthree" ?

 what do you mean "r" is firstname?

 <cfoutput>#r#</cfoutput>

Would display "John" ?


 <cfoutput>#one#</cfoutput>

Would display "Smith" ?

0
 

Author Comment

by:usky1
ID: 21868371
I have a URL that i am parsing. I strip out the individual names(sn#).
http://server.com/inds3.cfm?sn1=ronel&sn2=gtwo&sn3=bthreet
The structure is the first character is the first name (r) and all the rest are the last name (one). There can be one, two or three variables present. I am trying to find a way to identify all teh variables so I can update the database with the values.
I have created this loop to parse the information,
<cfset SNs = "">
<cfloop index="i" from="1" to="3">
      <cfif IsDefined("url.sn"&i)>
            <cfset intSN = Evaluate("url.sn"&i)>
            <!--- <cfset intSN = REReplace(intSN, "[^0-9]", "", "all")> --->
            <cfif intSN NEQ "">
                  <cfquery name="GetCSN" datasource="#request.dataSource#">
                        select * from ib_ChildReg where ID = #intID#
                  </cfquery>
                  <cfloop query="GetChildSN">
                        
                        <cfset SNs = ListAppend(SNs, intSN)>
                        <cfcookie name="sn_no" value="#SNs#">
                  </cfloop>
            </cfif>
      </cfif>
</cfloop>
And have stripped out the characters with the following,
<cfset fn1 = #Mid(sn1,1,1)#>
<cfset fn2 = #Mid(sn2,1,1)#>
<cfset fn3 = #Mid(sn3,1,1)#>
<cfset ln1 = #Mid(sn1,2,10)#>
<cfset ln2 = #Mid(sn2,2,10)#>
<cfset ln3 = #Mid(sn3,2,10)#>
I would like to see if I can do a loop for the full process and not worry if there one, two or three variables and set each one.
I hope this explains it. Please let me know if you need more information. thanks.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 21868522
Ok, it's getting a little clearer..  

For the first part, looping through your URL variables, you can loop through your query string like this.  

<cfset qstring = cgi.query_string>  <!--- the query string --->
<cfloop index="aPair" list="#cgi.query_string#" delimiters="&">
  <cfset v_name  = listFirst(aPair,"=")>
  <cfset v_value = listRest(aPair,"=")>
  <!---- verify that the name/value pair is like sn1, sn2, sn3 ---->
  <cfif left(v_name,2) is "sn" and isnumeric(trim(replace(v_name,"sn","")))>
       <!---- now you have v_name and v_value to work with ---->
       <cfset firstLetter = left(v_value,1)>
       <cfset lastName  = mid(v_value,2,len(v_value))>

  </cfif>
</cfloop>

The <CFIF > statement ensures that you are only working with sn# variables rather than anything else that may be on the URL at that time.

Now that we have the name/value pairs broken out, you want to split them into first lettter and rest of characters?

<cfset firstLetter = left(v_value,1)>
<cfset lastName  = mid(v_value,2,len(v_value))>

next?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:usky1
ID: 21874852
Thank you for the loop statment.
I've updated it to,
<cfset fns = "">
<cfset qstring = cgi.query_string>  <!--- the query string --->
<cfloop index="aPair" list="#cgi.query_string#" delimiters="&">
  <cfset v_name  = listFirst(aPair,"=")>
  <cfset v_value = listRest(aPair,"=")>
  <!---- verify that the name/value pair is like sn1, sn2, sn3 ---->
  <cfif left(v_name,2) is "sn" and isnumeric(trim(replace(v_name,"sn","")))>
       <!---- now you have v_name and v_value to work with ---->
       <cfset firstLetter = left(v_value,1)>
       <cfset lastName  = mid(v_value,2,len(v_value))>
         <cfset fns = ListAppend(fns, firstLetter)>
  </cfif>
</cfloop>

Should I be able to look at each individual vale from the listappend? For example,
<cfoutput>#fns2#</cfoutput>
Shouldn't I see the second value?
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 21875069
In the example you provided, if the URL contained
 sn1=rone&sn2=gtwosn3=bthree

Then your variables "fns" would end up with a value of  r,g,b
which is a list of the first letters, created by this line...

<cfset fns = ListAppend(fns, firstLetter)>

So far we have not set any values to a variable :  fns2

Here's where I am still a little lost.   Now that we have the firstletter and last name available to us in a loop, what is your objective with them?

I am wondering if you really need to create separate variables for each or do we just process each one inside the loop and at the end of the loop, we're done?

I've commented the attached code to show some questions, but rather than answering those, you could just try and give me the big picture of where you want to go.  For example,  I need a cookie to hold a list of last names,  I need to fetch from the database based on xxxxx and take this ID and....

I'm sure we'll get where you need to go, just a little foggy right now :)


Trying to translate what this does..
 
<cfif intSN NEQ "">
  <cfquery name="GetCSN" datasource="#request.dataSource#">
    select * from ib_ChildReg where ID = #intID# <=== where does intID come from?
  </cfquery>
  <cfloop query="GetChildSN"> <!==== where does GetChildSN come from?
    <cfset SNs = ListAppend(SNs, intSN)> <!=== is one of these from the query?
    <cfcookie name="sn_no" value="#SNs#"> <!=== SNs seems to hold the original values unparsed?
  </cfloop>
</CFIF>

Open in new window

0
 

Author Comment

by:usky1
ID: 21877105
This is the cycle I need to accomplish,
1. Parse the URL for id's and sn's
2. Parse each id and sn listed and then assign a variable number to them (for later use)
3. Parse the sn's to take out the firstname and lastname, for query exists or updates.
4. Verify through a query if the id does not exist then add it to the table.
5. Each row includes,
      cno(the same cno is used for all the variables)
      id, sn1(firstname) sn1(lastname). (this would loop through each id/sn)


<!==== where does GetChildSN come from?=========
<cfset SNs = "">
<cfloop index="i" from="1" to="3">
      <cfif IsDefined("url.sn"&i)>
            <cfset intSN = Evaluate("url.sn"&i)>
            <!--- <cfset intSN = REReplace(intSN, "[^0-9]", "", "all")> --->
            <cfif intSN NEQ "">
                  <cfquery name="GetChildSN" datasource="#request.dataSource#">
                        select * from ib_ChildReg where ID = #intID#
                  </cfquery>
                  <cfloop query="GetChildSN">
                        
                        <cfset SNs = ListAppend(SNs, intSN)>
                        <cfcookie name="sn_no" value="#SNs#">
                  </cfloop>
            </cfif>
      </cfif>
</cfloop>

<!=== is one of these from the query? Yes
<!=== SNs seems to hold the original values unparsed? Yes

Please let me know if you ahve any other questions.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 21877443
If you're ok with using an array, this code will populate an array of the values..

The array will be 1 to (the number of items on the URL)  regardless of the sn numbers...


<cfset fnsArray = arrayNew(1)>
<cfset qstring = cgi.query_string>  <!--- the query string --->
<cfloop index="aPair" list="#cgi.query_string#" delimiters="&">
  <cfset v_name  = listFirst(aPair,"=")>
  <cfset v_value = listRest(aPair,"=")>
  <!---- verify that the name/value pair is like sn1, sn2, sn3 ---->
  <cfif left(v_name,2) is "sn" and isnumeric(trim(replace(v_name,"sn","")))>
       <!---- now you have v_name and v_value to work with ---->
       <cfset firstLetter = left(v_value,1)>
       <cfset lastName  = mid(v_value,2,len(v_value))>
       <cfset fnsArray(arrayLen(fnsArray)) = v_value>
  </cfif>
</cfloop>
 
<cfdump var=#fnsArray#">

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 21877507
more comments on your version so to pin down where to go with my version...
<cfset SNs = "">
<cfloop index="i" from="1" to="3">
  <cfif IsDefined("url.sn"&i)>
    <cfset intSN = Evaluate("url.sn"&i)>
    <cfif intSN NEQ "">
       <cfquery name="GetChildSN" datasource="#request.dataSource#">
        select intSN from ib_ChildReg where ID = #intID# <=== don't you mean intSN ?  intID is not defined. Don't select * 
       </cfquery>
       --- this is faster than the CFLOOP --- 
       <cfset SNs = ListAppend(SNs, valueList(GetChildSN.intSN))> <=== is this the query's column name?  If so add query name as scope
    </cfif> 
  </cfif>
</cfloop>
-- if you want this to be ALL children of all SNs then it must be outof the loops
<cfcookie name="sn_no" value="#SNs#">

Open in new window

0
 

Author Comment

by:usky1
ID: 21878272
I am getting the following error,

Can not assign a value to a function.
Unable to assign a value to the function "fnsArray" on line 10, column 14

Line 10 is <cfset fnsArray(arrayLen(fnsArray)) = v_value>
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 21878356
sorry an array should have [ ]  not ( )

<cfset fnsArray[arrayLen(fnsArray)] = v_value>

0
 

Author Comment

by:usky1
ID: 21883911
I still ran into errors with the above so i was trying to figure out how to get all the variables I needed and came up up with the following,
<!--- group all id's for cookie --->
<cfset IDs = "">
<cfloop index="i" from="1" to="3">
      <cfif IsDefined("url.id"&i)>
            <cfset intID = Evaluate("url.id"&i)>
            <cfset intID = REReplace(intID, "[^0-9]", "", "all")>
            <cfif intID NEQ "">
                  <cfquery name="GetChild" datasource="#request.dataSource#">
                        select * from ib_ChildReg where ID = #intID#
                  </cfquery>
                  <cfloop query="GetChild">
                        <cfcookie name="customer_no" value="#GetChild.customer_no#">
                        <cfset IDs = ListAppend(IDs, intID)>
                  </cfloop>
            </cfif>
      </cfif>
</cfloop>
<cfif IDs NEQ "">
      <cfcookie name="ChildIDs" value="#IDs#">
</cfif>

<!--- group all sn's for cookie --->
<cfset SNs = "">
<cfloop index="i" from="1" to="3">
      <cfif IsDefined("url.sn"&i)>
            <cfset intSN = Evaluate("url.sn"&i)>
            <cfif intSN NEQ "">
                  <cfquery name="GetChildSN" datasource="#request.dataSource#">
                        select * from ib_ChildReg where ID = #intID#
                  </cfquery>
                  <cfloop query="GetChildSN">
                        <cfset SNs = ListAppend(SNs, intSN)>
                        <cfcookie name="ChildSNs" value="#SNs#">
                  </cfloop>
            </cfif>
      </cfif>
</cfloop>

<!--- set all id/sn to individual variables  --->
<cfset myarray=ListToArray(#CGI.QUERY_STRING#, "&")>
<!--- Dumb for testing purposes --->
<cfdump var="#myarray#" expand="yes">

<cfloop index="ax" from="1" to="#ArrayLen(myarray)#">
      <!--- <cfoutput>#myarray[ax]#</cfoutput> --->
    <cfset variable=#ListToArray(#myarray[ax]#,"=")#>
    <!--- <cfdump var="#variable#" expand="yes"> --->
</cfloop>
<cfset fn1 = left(sn1,1)>
<cfset ln1  = mid(sn1,2,len(sn1))>
<cfset fn2 = left(sn2,1)>
<cfset ln2  = mid(sn2,2,len(sn2))>
<cfset fn3 = left(sn3,1)>
<cfset ln3  = mid(sn3,2,len(sn3))>

<!--- I have all the first and last names identified --->
<cfoutput>
ids=#ids#<p>
sns=#sns#<p>
fn1=#fn1#<p>
ln2=#ln1#<p>
fn2=#fn2#<p>
ln2=#ln2#<p>
fn3=#fn3#<p>
ln3=#ln3#<p>
</cfoutput>

<!--- 1. How do I loop through all the firstnames(fn's) and lastnames(ln's) to see
if already exists in the database?
2. If no exists then how do I loop through all the firstnames(fn's) and lastnames(ln's) to insert into
the database? --->

<cfset qArray = listToArray(ids) />
<cfloop from="1" to="#arraylen(qArray)#" index="i">
     <cfquery name="AddUser" datasource="#request.dataSource#">
          INSERT INTO CReg (cusno,IID,sn1)
          VALUES ( '#cno#','#qArray[i]#');
     </cfquery>
</cfloop>
0
 

Author Closing Comment

by:usky1
ID: 31470570
Thank you for your help. I have learned something new, always a good thing. Can you recommend and books, beside the Adobe documentation, that will help me understand the functions i was trying to do?
0

Featured Post

Don't miss ATEN at NAB Show April 24-27!

Visit ATEN at NAB Show to learn how our "Seamlessly Entertaining" solutions deliver fast, precise video streaming without delays for the broadcasting and media environment. ATEN will showcase its 16x16 Modular Matrix Switch (VM1600) and KVM Over IP Solution (KE6900 series).

Question has a verified solution.

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

Suggested Solutions

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

762 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