Coldfusion ListToArray and using "==" as a delimiter - coldfusion

The code:
<cfset LOCAL.temp = 'something==a descript >= ive value' />
<cfdump var="#ListToArray(LOCAL.temp, '==')#" />
What I expect is an array with two indices what I get is an array with three indices, CF is also splitting at the single equals sign.
Is anyone else experiencing this behavior or can explain what is going on?

This is expected behaviour. As described in the docs, ListToArray uses single-character delimiters (by default).
One solution is to use split:
Temp.split('==')
(This is making use of the underlying Java string.split method, which splits a string at every match of a specified regex, and works on all Java-based CFML engines, though produces a Java String Array - which can't be directly manipulated with CF's ArrayAppend and related functions, unless it is first converted. ).
Since you're on CF9, you can also use the new multi-char delimiter flag, by setting the fourth argument to true:
ListToArray(Temp,'==',false,true)

Related

How to avoid backslash in middle of a regular expression extracted value Load runner and sent in the Request body parameter

In response, an authentication value consists of \ to escape / in parameter so while capturing parameter it is getting "\" in middle as well but in subsequent request need to send with out "\" is there any way to do this in LoadRunner
Example :-
web_reg_save_param_ex(
"ParamName=pValue",
"LB=Value:",
"RB=\"",
SEARCH_FILTERS,
"Scope=Body",
LAST);
Captured Value is AdfjshxnjkAKLDKLJlk\/ghg
Required value is AdfjshxnjkAKLDKLJlk/ghg
How to remove \ this from the value.
Is there any load runner inbuilt functions for this.
I had a similar problem that I solved by storing the correlated parameter as a string variable and then using a replace function to parse and replace the characters I didn't need.
Only problem is I was using JavaScript as my scripting language in VuGen so my code specifics wouldn't help you much. You might see about doing the same thing with C, or if switching to JS is plausible for you, mine looked similar to this:
var str = lr.evalString("{correlated_parameter}")
var corrected_string = str.replace(/\\/g, '');
My code used a different regular expression, but I think I have the syntax right for what you're trying to do, but I haven't tried this exact string of course.
Here's a link to another SO thread with more details on using the replace function.

Is there a way to exact match "truthy" and "falsey" values in ColdFusion

I recently had the need to match against two strings in ColdFusion and ran into this scenario during my loop:
<cfif "0" IS NOT "NO">
Generally during the loop it looks something like this:
<cfif "AM" IS NOT "BA">
Now both of these values were variables (I wasn't just typing it out for fun) and I was using "0" as a default value for the first variable to match against (since the second variables would never be 0) but both of these values changed in the loop I was running. I easily fixed this by setting my default value to -- instead of 0 but I tried researching and found nothing indicating there was a way to get around the falsey nature of strings when evaluating them.
Is there no Operator or trick to match on the strings themselves and ignore their truthyness or falseyness in ColdFusion?
The compare function will help you. This:
writedump(compare("0", "NO"));
returns -1.
This page will tell you what that means.

Using hyphens in argument names

I am working with CFWheels and jquery mobile and am trying to pass some jquerymobile settings into a linkto call (mainly the data-icon attribute. I never new this before, but it appears to be that ColdFusion doesn't allow hyphens in argument names. My call is as follows:
<cfset contentFor(actioncontent=linkTo(text='Login', route='login', data-icon='check')) />
CFBuilder and Railo throw an error on the hyphen. The Railo error is:
invalid assignment left-hand side (railo.transformer.bytecode.op.OpDouble)
So my questions is: am I correct in saying that hyphens are not allowed in argument names? Also if they are not allowed, is there a way to get the hyphen through or do I just have to create the anchor tag?
try using quotes 'data-icon' or doublequotes "data-icon"
It's being interpreted as a minus not a dash
You can get this to work on Railo and Adobe's CF by creating a struct first and sending it in the argument collection. Otherwise, it will only work on Railo.
Example:
<cfscript>
args = {controller="form",
'data-role'="button",
'data-theme'="c",
text="I Accept"};
</cfscript>
#linkTo(argumentCollection=args)#
My quick hack is this:
#replace(linkTo(text="I accept", route="dashboard"),"<a ","<a data-role='button' ","ALL")#
(emphasis on the work hack - not ideal, but much easier than passing in the argumentCollection).

What is the most direct way to reproduce this printf-like format with ColdFusion?

I've been thrown into ColdFusion for a very simple assignment. The application has some logic to display "help codes" (let's not get into what is a help code), however, the logic is buggy and needs to be fixed. Given a two-letters code, a 1-4 digits number, and another 1-2 digits number, I would need to display them like this printf call would:
printf("%s%04d%02d", letterCode, bigNumber, smallNumber);
If you're not familiar with the printf function, it accepts a format string (the first parameter), and writes the other variables in it according to the given format. %s means "write a string" and %d means "write a number"; %0zd means "write a number and pad it with zeroes so it's at least z characters long (so %04d means "write a number and pad it with zeroes so it lengths at least 4 digits).
Here are a few examples with %s%04d%02d:
"AD", 45, 12: AD004512
"GI", 5121, 1: GI512101
"FO", 1, 0: FO000100
However, it's my very first time with ColdFusion, and I couldn't find anything like printf or sprintf to format strings.
The other guy, who doesn't work here anymore, resorted to a (non-working) loop, and I thought it would be better to use library code instead of actually fixing the loop, since anyways I might need to do similar things again.
<cfset bigNumberPadded = NumberFormat(bigNumber,"0000")>
<cfset smallNumberPadded = NumberFormat(smallNumber,"00")>
<cfoutput>#letterCode##bigNumberPadded##smallNumberPadded#<cfoutput>
Or alternatively... as suggested by bpanulla, and corrected by Leigh
<cfset args = ["AD", javacast("int", 45), javacast("int", 12)]>
<cfset output= createObject("java","java.lang.String").format("%s%04d%02d", args) >
You can use NumberFormat to pad a number with leading zeros in CF.
<cfoutput>#letterCode##NumberFormat(bigNumber, '0000')##NumberFormat(smallNumber, '00')#</cfoutput>
There's lots of ways to do this in the Java layer underpinning ColdFusion. Here's one Java resource:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
Make instances of the Java classes you need with CFOBJECT or CreateObject.
I am assuming you are displaying these to a webpage? If so, I would use a switch/case statement. Since you said "given a two-letter code...", a switch/case would work well. For example:
<cfswitch expression="#twoLetterCode#">
<cfcase value="aa12348">%s%04d%02d</cfcase>
<cfcase value="bb23456">%s%05f%01e</cfcase>
<cfcase value="cc97641">%s%08g%10j</cfcase>
<cfdefaultcase>%s%04d%02d</cfdefaultcase>
</cfswitch>
Or you could use an if/else instead. But the main point (to answer your question) is that in ColdFusion you just type out the display characters (be it help codes, or text, or whatever). You don't need to use a special function to display text to the page.

Element-in-List testing

For a stylesheet I'm writing (actually for a set of them, each generating a different output format), I have the need to evaluate whether a certain value is present in a list of values. In this case, the value being tested is taken from an element's attribute. The list it is to be tested against comes from the invocation of the stylesheet, and is taken as a top-level <xsl:param> (to be provided on the command-line when I call xsltproc or a Saxon equivalent invocation). For example, the input value may be:
v0_01,v0_10,v0_99
while the attribute values will each look very much like one such value. (Whether a comma is used to separate values, or a space, is not important-- I chose a comma for now because I plan on passing the value via command-line switch to xsltproc, and using a space would require quoting the argument, and I'm lazy-enough to not want to type the extra two characters.)
What I am looking for is something akin to Perl's grep, wherein I can see if the value I currently have is contained in the list. It can be done with sub-string tests, but this would have to be clever so as not to get a false-positive (v0_01 should not match a string that contains v0_011). It seems that the only non-scalar data-type that XSL/XSLT supports is a node-set. I suppose it's possible to convert the list into a set of text nodes, but that seems like over-kill, even compared to making a sub-string test with extra boundaries-checking to prevent false matches.
Actually, using XPath string functions is the right way to do it. All you have to make sure is that you test for the delimiters as well:
contains(concat(',' $list, ','), concat(',', $value, ','))
would return a Boolean value. Or you might use one of these:
substring-before(concat('|,' $list, ',|'), concat(',', $value, ','))
or
substring-after(concat('|,' $list, ',|'), concat(',', $value, ','))
If you get an empty string as the result, $value is not in the list.
EDIT:
#Dimitre's comment is correct: substring-before() (or substring-after()) would also return the empty string if the found string is the first (or the last) in the list. To avoid that, I added something at the start and the end of the list. Still contains() is the recommended way of doing this.
In addition to the XPath 1.0 solution provided by Tomalak,
Using XPath 2.0 one can tokenize the list of values:
exists(tokenize($list, ',')[. = $value])
evaluates to true() if and only if $value is contained in the list of values $list