Quantcast
Channel: Adobe Community : Unanswered Discussions - ColdFusion
Viewing all articles
Browse latest Browse all 5794

How to add a character at the end of an array element that meets certain criteria.

$
0
0

Hi. I am using CF 9.2.1. I started learning CF yesterday, and I am trying to figure out a way to add a special character (like "?", "!"," & ") to an array row if the first row element (say, element [1][1]) has a specific letter like " t" in the word it is holding. In this case the special character will be added at the end of the last row element (element [1][3]).

 

Example: The take columns are: "Firstname", "Lastname", "Age".  If Firstname contains an "s" or "S" (in any order) then add "!" at the end of the row so that :

 

Sam, Rubins, 26 !

Nick, Palo, 32

Robert, Williams, 28

Oscar, Ramirez, 23 !

 

I tried using the ReReplace, Refind and the #findoneof functions. I could probably loop through the array and return a value and store, while storing the value in a separate variable and using an if/Boolean function to compare that value and so on.... But I am sure there's an easier and more efficient way. Thanks in advance.

 

 

<!--- #1 I will create a three column query named CityInfo--->

<cfset CityInfo = QueryNew("City, State, LCode","VarChar, VarChar, Integer")>

<cfset newRow = QueryAddRow(CityInfo, 2)>

<cfset newRow = QueryAddRow(CityInfo, 3)>

<cfset newRow = QueryAddRow(CityInfo, 4)>

<cfset newRow = QueryAddRow(CityInfo, 5)>

<cfset newRow = QueryAddRow(CityInfo, 6)>


<!---Using the column names City, Sate and LCode I will now enter the information--->

<cfset temp = QuerySetCell(CityInfo, "City", "Dallas", 1)>

<cfset temp = QuerySetCell(CityInfo, "State", "TX", 1)>

<cfset temp = QuerySetCell(CityInfo, "LCode", "214", 1)>


<cfset temp = QuerySetCell(CityInfo, "City", "Fort Worth", 2)>

<cfset temp = QuerySetCell(CityInfo, "State", "TX", 2)>

<cfset temp = QuerySetCell(CityInfo, "LCode", "817", 2)>


<cfset temp = QuerySetCell(CityInfo, "City", "San Antonio", 3)>

<cfset temp = QuerySetCell(CityInfo, "State", "TX", 3)>

<cfset temp = QuerySetCell(CityInfo, "LCode", "210", 3)>


<cfset temp = QuerySetCell(CityInfo, "City", "L.A.", 4)>

<cfset temp = QuerySetCell(CityInfo, "State", "CA", 4)>

<cfset temp = QuerySetCell(CityInfo, "LCode", "213", 4)>


<cfset temp = QuerySetCell(CityInfo, "City", "Austin", 5)>

<cfset temp = QuerySetCell(CityInfo, "State", "TX", 5)>

<cfset temp = QuerySetCell(CityInfo, "LCode", "512", 5)>


<h4> Number1. City info TABLE contents:</h4>

<cfoutput query = "CityInfo">

#City# #State# #LCode#<br>

</cfoutput> 



<!--- #2 Now I focus on the array. I declare it first --->

<cfset cityarray=arraynew(2)>

<!--- Then I retrieve the data and populate the array --->

<cfloop query="CityInfo">

<cfset cityarray[CurrentRow][1]=City>

<cfset cityarray[CurrentRow][2]=State>

<cfset cityarray[CurrentRow][3]=LCode>


</cfloop>

<h4>Number2. City info ARRAY contents before appending:</h4>


<cfloop index="Counter" from=1 to=5>

<cfoutput>

City: #cityarray[Counter][1]#,

Estate: #cityarray[Counter][2]#,

Code: #cityarray[Counter][3]#<br>

</cfoutput>

</cfloop>


<!--- #3 I now add/append Irving to the array and change L.A. --->

<cfset cityarray[6][1] = "Irving" />

<cfset cityarray[6][2] = "Texas" />

<cfset cityarray[6][3] = "972" />

<cfset cityarray[4][1] = "Los Angeles" />


<h4>Number3. City info Array contents after adding Irving, TX and updating L.A.:</h4>


<cfloop index="Counter" from=1 to=6>

<cfoutput>

City: #cityarray[Counter][1]#,

Estate: #cityarray[Counter][2]#,

Code: #cityarray[Counter][3]#<br>

</cfoutput>

</cfloop>

<br>



<!--- #4 now I look for the 's' in order to add a '!' THIS IS WHERE MY PROBLEM BEGINS --->

<h4>Number4. City info after searching for "s"</h4>



<cfloop index="Counter" from=1 to=6>


<cfoutput>

City: #cityarray[Counter][1]#,

Estate: #cityarray[Counter][2]#,

Code: #cityarray[Counter][3]#,

<!--- I know the findoneof function is not going to help me, but I hope it helps to give you an idea of what I am trying to do--->

#findoneof("sS",cityarray[Counter][1])#

<br>

</cfoutput>

</cfloop>


Viewing all articles
Browse latest Browse all 5794

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>