Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Assignment using CFLOOP (Classic) 1

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

This isn't very hard, I just can't get my syntax together. Here's my goal - Split a string containing a space into two parts.

Example. Fullname = 'George Jetson'
I want Fname = 'George' and Lname = 'Jetson'.

Here's my code:

<html>
<head>
<title>Test String Looping</title>
</head>

<body>
<cfset string = &quot;George Jetson&quot;>
<cfset fname=&quot;&quot;>
<cfloop from=&quot;1&quot; to=&quot;#len(string)#&quot; index=&quot;ThisChar&quot;>
<!--- <cfoutput>#Mid(string,ThisChar,1)#</cfoutput> --->
<!--- If the character is not blank, dump it in first name --->
<cfif #Mid(string,ThisChar,1)# eq ' '>
<!--- Variable Assignments Here --->
<cfoutput>#fname#</cfoutput>
<cfoutput>#lname#</cfoutput>
</cfif>

</cfloop>


</body>
</html>


Thanks in advance for your help.

scripter73
 
You can probably make that work but there is a much easier way.

Try this:
<CFSET words=&quot;This That&quot;>

<CFSET FirstWord = #Listgetat(words,1,&quot; &quot;)# >
<CFSET SecondWord = #Listgetat(words,2,&quot; &quot;)# >

As long as there is always and only a space between the words you can call it a list and access them as if it is a viariable. This will work with 2 words or 200.

Have fun.
 
A simpler way to do it is to treat the name as a &quot;list&quot; seperated by spaces instead of commas. With that in mind, you can use list functions and specify the deliminator as a space &quot; &quot;

<CFSET string = &quot;George Jetson&quot;>
<CFSET fname = ListFirst(string,&quot; &quot;)>
<CFSET lname = ListLast(string,&quot; &quot;)>

<CFOUTPUT>
First Name: #fname#<BR>
Last Name: #lname#
</CFOUTPUT>


Returns:
First Name: George
Last Name: Jetson

This would even work if they had a long name:

&quot;George Rodrigues Anthony Jetson&quot; with the above script would return:

First Name: George
Last Name: Jetson
- tleish
 
Thanks for everybody's help. I hope this question helps alot of people. Here's what I got too, but I really like the other two options.

<html>
<head>
<title>Test String Looping</title>
</head>

<body>
This tiny program splits a string that's a fullname into 2 parts - Fname and Lname.<br>
<cfset string = &quot;George Jetson&quot;>
Thus <cfoutput>#string#</cfoutput> becomes:<br><br>

<!--- Find whitespace position of string. --->
<cfset pos = Find(&quot; &quot;, string, 1)>

<!--- Variable Assignments Here --->
<cfset fname=Left(string, pos)>
<cfset lname=Right(string,pos)>
<cfoutput>Fname = #fname#</cfoutput><br><br>
<cfoutput>Lname = #lname#</cfoutput><br><br>

</body>
</html>

Thanks again!
scripter73
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top