• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

REFind variables

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
usky1
Asked:
usky1
  • 6
  • 6
1 Solution
 
gdemariaCommented:
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
 
usky1Author Commented:
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
 
gdemariaCommented:
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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
usky1Author Commented:
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
 
gdemariaCommented:
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
 
usky1Author Commented:
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
 
gdemariaCommented:
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
 
gdemariaCommented:
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
 
usky1Author Commented:
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
 
gdemariaCommented:
sorry an array should have [ ]  not ( )

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

0
 
usky1Author Commented:
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
 
usky1Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now