• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Apr 02, 2014 Apr 02, 2014

Copy link to clipboard

Copied

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>

TOPICS
Getting started

Views

679

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2014 Apr 02, 2014

Copy link to clipboard

Copied

Just 2 things.

1) QueryAddRow

<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)>

This creates 20 rows, whereas you want just 5. The second argument of the function represents the total number of  rows! So you require just the one statement: <cfset newRow = QueryAddRow(CityInfo, 5)>

2) FindOneOf

Nothing wrong with findoneof("sS",cityarray[Counter][1]). However, I think findNoCase("s",cityarray[Counter][1]) is conceptually simpler, as it is case-insensitive.

If this is what you can do after one day of ColdFusion, then you will work wonders in future, bro!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 02, 2014 Apr 02, 2014

Copy link to clipboard

Copied

Hey man, thanks. Your feedback is very useful and greatly appreciated. I am a physicist, currently interviewing for a job in IT. The company uses mostly JQuery, MySQL, Python and ColdFusion. They gave me a test yesterday afternoon to see if I could learn the CF language fast enough. This is the test and I've done everything possible to get all points of it, you've been of great help on that now.  My biggest problem is still standing though: how to add the character "!" at the end of the rows that contain an "s" in the city name.  My current probable solution is to try and test each row, return a value (1/0 or true/false) then store that value in a variable and use an if statement to add the semi colon. Not only is this more complicated but I doubt that I will be able to even find and learn all the information to build that function. Am sure there's an easier way, like a set of functions, to add the "!" at the end of the row that has city with an "s" in it. I tried a few things and learned a lot, but just not what I need.Would you know?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2014 Apr 02, 2014

Copy link to clipboard

Copied

It is much simpler than you think. Additions and modifications are in italics. Good luck!

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

<cfloop index="Counter" from="1" to="#CityInfo.recordcount#">

<cfoutput>

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

State: #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="#arrayLen(cityarray)#">

<cfoutput>

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

State: #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="#arrayLen(cityarray)#">

    <cfset sPosition=findNoCase("s",cityarray[Counter][1])>

    <cfoutput>

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

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

    Code: #cityarray[Counter][3]#

    </cfoutput>

   <cfif sPosition GT 0><!--- Just display the exclamation mark--->

    !

    <cfelse> <!--- Add whatever you like, or nothing, here--->

    (no 's')

    </cfif>

    <br>   

</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2014 Apr 02, 2014

Copy link to clipboard

Copied

I assumed in my last post you only wish to display the result. However, if you may also wish to store the result in a variable, for example, for reuse. You could use a list, as follows:

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

<cfset cityInfoList="">

<cfloop index="Counter"  from="1" to="#arrayLen(cityarray)#">

    <cfset sPosition=findNoCase("s",cityarray[Counter][1])>

    <cfset cityInfo="City: #cityarray[Counter][1]#, State: #cityarray[Counter][2]#, Code: #cityarray[Counter][3]#">

    <cfif sPosition GT 0><!--- I have used the character / as delimiter (list separator) --->

        <cfset cityInfoList=listAppend(cityInfoList,cityInfo & "!","/")>

    <cfelse>

        <cfset cityInfoList=listAppend(cityInfoList,cityInfo,"/")>

    </cfif>

</cfloop>

<cfloop list="#cityInfoList#" index="listElement" delimiters="/">

    <cfoutput>#listElement#</cfoutput><br>

</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 03, 2014 Apr 03, 2014

Copy link to clipboard

Copied

This language is awesome. I knew about using #arraylen()# but like you said, I was using too many rows to begin with, and that was a pain. You won't believe it bro, but not knowing that I could set a variable like sPosition equal to FindNoCase  was my biggest problem:

sPosition=findNoCase("s",cityarray[Counter][1])

In other words, I did not think the findNoCase could be used as a function and store the value returned. Thanks for also taking the extra time to show me the "list" trick, I learned about the list, but I guess I was rushing through the CF material trying to figure my main problem out. One last thing, you rock man! What I needed was a little guidance, but you went the extra mile. Thanks for your time, it was truly appreciated, and I wish you the best of luck too.

-Omar

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2014 Apr 04, 2014

Copy link to clipboard

Copied

LATEST

Cheers, Omar!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation