How would I use use a string function to select everything in a variable after the last "/"
http://domain.com/g34/abctest.html
So in this case I would like to select "abctest.html"
Running ColdFusion 8.
Any suggestions?
Um, a bit strange to give very similar answer within few days, but ListLast looks as most compact and straightforward approach:
<cfset filename = ListLast("http://domain.com/g34/abctest.html","/") />
And yes, IMO you should start with this page before asking such questions on SO.
Joe I'd use the listToArray function, passing the "/" as the delimiter, get the length of the array and get the value in the last slot. See sample below
<cfset str = "http://domain.com/g34/abctest.html"/>
<cfset arr = ListToArray(str,"/","false")/>
<cfset val = arr[ArrayLen(arr)]/>
<cfoutput>#str# : #val#</cfoutput>
produces
http://domain.com/g34/abctest.html : abctest.html
Related
I have an array:
<cfset fullarray = listtoArray (listofcollumnvalues)>
And it had for example:
fullarray[1]=20
fullarray[2]=11
fullarray[3]=4
fullarray[4]=12.2
etc.
And I wanted to add the sum of the the X position below number:
for example if I wanted the sum of the second element and below
2 + 3 + 4= 27.2
ColdFusion 10 introduced the ArraySlice function. It returns an array when you give it an array, a starting location, and an optional length. So ArraySlice(myArray,3,4) would return a "sub-array" with the elements that start at position 3 and includes 4 elements.
Based on your example:
mySum = ArraySum(ArraySlice(fullarray,2))
If you're on CF 9 or below, you can use a UDF. At CFLib.org there is
arraySlice
arraySlice2
Warning! arraySlice uses "start" and "end" element arguments, while arraySlice2 uses "start" and "length" arguments, like the built-in CF10 function.
Here's something that will work on older versions.
<cfscript>
function listSum(listStr)
{
var delim = ",";
if(ArrayLen(Arguments) GTE 2)
delim = Arguments[2];
return ArraySum(ListToArray(listStr, delim));
}
</cfscript>
Simply envoke it as #listSum(MyListOrArray)#
This is a formatted comment. You define your array as this:
<cfset fullarray = listtoArray (listofcollumnvalues)>
The variable, listofcollumnvalues looks like it might represent results of a query object. If that's the case, You might be doing more work than necessary, because each column of a query is effectively an array. Consider for example this query:
<cfquery name = "queryName">
select field1, numericalField
from etc
</cfquery>
You can actually do stuff like this:
numericalFieldSum = ArraySum(queryName['numericalField'];
It's not exactly what you want, but it illustrates that you might not need your initial command.
Continuing with the assumption that you started with a query, a query of queries might give you what you want.
<cfquery name = "mySum" dbtype = "query">
select sum(numericalField) theSumIWant
from queryName
where your conditions are met
</cfquery>
Of course, if listofcollumnvalues does not come from a query, you should declare this answer to be useless twaddle and downvote accordingly.
I am trying to change the delimiters of a list. So far I have been successful, but am losing ground on one of the actions.
The issue is the with the last element, which is v. The code never appends ="" to the last element. It should not add a comma to it.
Code:
<cfset foo="t,u,n,f,o,a,c,r,v">
<cfset msg = ListChangeDelims(foo,'="",')>
<cfoutput>#msg#</cfoutput>
Result:
t="",u="",n="",f="",o="",a="",c="",r="",v
I hope my question makes sense.
That is the definition of delimiter. Anyway, it is relatively easy to fix, just add the last one yourself.
<cfset foo="t,u,n,f,o,a,c,r,v">
<cfset msg = ListChangeDelims(foo,'="",')>
<cfset msg &= '=""'>
<cfoutput>#msg#</cfoutput>
http://trycf.com/gist/4ac3702b74bd79b5d1f8/
There is always the old fashioned way
msg = replace(foo,",","=", "all");
ColdFusion is converting a string to number when passing to JS via a SerializeJSON and deSerializeJSON.
This is only happening when an 'E' is used between two set of numbers. like 3E6, 65E3, 56e45 etc. This is the code inside cfscript.
x = "2e9";
writedump(SerializeJSON(x));
writedump(deSerializeJSON(SerializeJSON(x)));
Output:
2.0E9 2000000000
Please suggest, if is there any other way for such issues.
It is this: https://bugbase.adobe.com/index.cfm?event=bug&id=3695627: "SerializeJSON turns strings that look like scientific notation into floats."
It's a known bug in CF9, and it's fixed in CF10.
In the meantime, you will just have to pad the string with something to force ColdFusion to not see it as a number in scientific notation.
Or upgrade to CF10 (CF9 is end of life next month, btw). Or to Railo.
I have resolved this using below solution
let's say ItemUnit = 12E45
stcReturn.firstname = "Yes";
stcReturn.lastname = "Man";
stcReturn.ItemUnit = "12E45"
error output after deSerializeJSON stcReturn.ItemUnit = 12e+46
<cfset stcReturn.ItemUnit = ItemUnit />
<cfset StructSetMetaData(stcReturn.ItemUnit, {ItemUnit :
{"type":"string","name":"ItemUnit"}})/>
deSerializeJSON(stcReturn)
Correct Output:12E45
I'm a bit stumped on this one..
I currently have a string.
Please enter your variable.firstname here
What i would like to do is find the variable.firstname in the string and convert it to be used as #variable.firstname#
Im using CF8, and ive looked at using findNoCase() but the variable.firstname portion can appear anywhere. I am also trying to use this in a Coldfusion Custom Tag as its to simply display the firstname of the user that could be dynamically populated.
I cant use any other functionality to change it IE = variable['firstname] because the variable could be the result of a dynamic variable i pass in and the query for the content will reside within the custom tag.
<cfset yourNewString = replace(yourOldString,'variable.firstname',
'##variable.firstname##', 'all')>
Note the double pound signs.
I cant use any other functionality to change it IE =
variable['firstname] because the variable could be the result of a
dynamic variable i pass in and the query for the content will reside
within the custom tag.
I'm not sure I understand exactly what you're saying here but if you're saying that variables.firstname is coming from another variable and the .firstname is the dynamic part you could still use array notation.
<cfset myName = "Travis">
<cfset yourName = "user125264">
<cfset myCustomVariable = "myName">
<cfoutput>Hi, My name is #variables[myCustomVariable]#. </cfoutput>
<cfset myCustomVariable = "yourName">
<cfoutput>Your name is #variables[MyCustomVariable]#.</cfoutput>
Output: Hi, My name is Travis. Your name is user125264.
If that isn't what you meant, I apologize.
If you're trying to replace variable.firstname with #variables.firstname# and then also get the value of that variable, you'll need to do the replace <cfset yourNewString = replace(yourOldString,'variable.firstname',
'##variables.firstname##', 'all')> and then wrap the resulting string in an evaluate() function (and an inner de() to prevent CF from evaluating everything): <cfset evaluatedString = evaluate(de(yourNewstring))>.
If there are more variables besides variable.firstname that need this kind of translation, you'll need to get into regex with reReplace() to catch them all in one statement. My regex is rusty, so you'll need to Google that bit on your own. ;o)
This is my code:
returnStruct.myList = myList;
returnStruct.first = trim(ListGetAt(myList,3));
returnStruct.last = trim(ListGetAt(myList,13));
returnStruct.address = trim(ListGetAt(myList,15));
returnStruct.city = trim(ListGetAt(myList,2));
returnStruct.state = trim(ListGetAt(myList,9));
Everything is working fine until myList hits empty values and then everything crashes.
I found a command "includeEmptyValues" that I can set to 'yes' but I am not familiar with it and the documentation of ColdFusion 9 isn't the best I've come across.
http://cfquickdocs.com/cf9/#listgetat
Previous versions of ColdFusion (and CF9 by default) counted consecutive delimiters as a single delimiter. So a list that looked like this:
<cfset myList="a,b,,c,,d" />
was considered to have four elements.
Recently added is the "includeEmptyValues" attribute.
listGetAt(list, position [, delimiters, includeEmptyValues ])
So while
<cfset myVar=listGetAt(myList,6) />
will throw an error
<cfset myVar=listGetAt(myList,6,",","true") />
will successfully set myVar to d.
might want to use listToArray(), and ArrayIsDefined(). Play with includeEmptyFields attr and see which behavior you prefer. True = Convert empty elements in a list to empty array entries