Compare string "A" to string "B" in coldfusion - coldfusion

Is there a function in Coldfusion that will take 2 strings and figure out which is the 'higher' alphabetically . So if I had "Daniel" and "John", it would return Daniel?

Put your strings into an array then use arraySort(). (example not tested)
var names = ['Daniel', 'John'];
arraySort( names, 'textnocase' );
writeOutput(names[1]);

Related

For a Boost.Propertytree, is there any way to use JSON dot notation to reference an array element?

It would be great to be able to specify a path into a Boost.PropertyTree containing an array.
I can construct a Boost.PropertyTree from this JSON:
const char* theJSONTestText = R"FooArrayTest({
"FooArray": [
{"BarIntValue": 10, "BarString": "some string"},
{"BarIntValue": 99, "BarString": "another string"},
{"BarIntValue": 45, "BarString": "a third string"}
]
})FooArrayTest";
Which is constructed, and prints as expected:
FooArray:
:
BarIntValue: 10
BarString: some string
:
BarIntValue: 99
BarString: another string
:
BarIntValue: 45
BarString: a third string
Of course, the individual elements of the array don't have names.
I know HOW to iterate through the FooArray property, but it would be especially convenient to be able to access individual elements via a JSON dot notation path, like "FooArray[2].BarString", to access a field in the third array element:
std::string theSecondBarString = theParsedTree.get<std::string>("FooArray[2].BarString");
Of course, this throws an exception, because I'm guessing Boost.PropertyTree doesn't know how to handle a path with an array specifier? Or do I have the syntax wrong?
WHY DO I WANT TO DO IT THIS WAY?
I want the client of this PropertyTree to not only be able to GET data from the a specific array element, but also to SET (i.e. change) the data of a specific array element. If there's no straightforward path notation, then the client has to use my invented API functions to first extract then access the desired field, and the reverse to write it back out. That can be tedious and error-prone for tree nodes that contain array elements within array elements.
Incredibly --
This syntax constructs exactly what I'm looking for:
const char* theJSONTestText = R"FooArrayTest({
"SomeArray": {
"[1]": {"BarIntValue": 10, "BarString": "some string"},
"[2]": {"BarIntValue": 99, "BarString": "another string"},
"[3]": {"BarIntValue": 45, "BarString": "a third string"}
}
})FooArrayTest";
...which creates a PropertyTree like this:
DUMP OF parsed JSON:
SomeArray:
[1]:
BarIntValue: 10
BarString: some string
[2]:
BarIntValue: 99
BarString: another string
[3]:
BarIntValue: 45
BarString: a third string
...and allows a syntax such as:
std::string theSecondBarString = theParsedTree.get<std::string>("SomeArray.[2].BarString");
...and -- VOILA:
Second bar string = another string
IMPORTANT: it must be noted that this approach abandons the array notation "[", "]" in the original JSON definition text. Instead, I am simply creating CHILD NODES of SomeArray with the names "[n]". Each child node ([1], [2], [3]) have their OWN child nodes with BarIntValue and BarString. Not what I expected, but it WORKS!
Now I just need to figure out how to construct that PropertyTree using the member functions (vs the raw JSON) and I'm gold!

Capitalize the first string in list using python

I have a list containing strings and I want capitalize the first letter of first string using Python and not the entire list.
I have attempted the following but every first letter in the list is capitalized:
L = ("hello", "what", "is", "your", "name")
LCaps = [str.capitalize(element) for element in L)
print LCaps
So, you want to capitalize the first string and only the first string of a tuple. Use:
>>> L = ("hello", "what", "is", "your", "name")
>>> (L[0].capitalize(),) + L[1:]
('Hello', 'what', 'is', 'your', 'name')
Key points:
Strings have methods. There is no need to use the string module: just use the strings capitalize method.
By running L[0].capitalize(), we capitalize the first string but none of the others.
Because L is a tuple, we can't change the first string in-place. We can however capitalize the first string and concatenate it with the rest.
(L[0].title(),) + L[1:]
You can use title() too.

Spliting string into a list of substrings

I have a string id <- "Hello these are words N12345678 hooray how fun".
I would like to extract just N12345678 from this string.
So far I have used strsplit(id, " "). Now I have
>id
>[[1]]
>[1] "Hello" "these" "are" "words" "N12345678" "hooray" "how"
>[8] "fun"
Which is of type list and of length 1 (despite apparently having 8 elements?)
If I then use id <- id[grep("^[N][0-9]",id)],
id is an empty list.
I think what I need to do is split the string into a list of length 8 with each element as a substring and then grep should be able to pick out the pattern, but I'm not sure how to go about that.
Use regmatches
> regmatches(id, regexpr("N[0-9]+", id))
[1] "N12345678"
If you insist on using strsplit. I think this can solve the problem:
id <- "Hello these are words N12345678 hooray how fun"
id = strsplit(id, " ")
id[[1]][grep("^N[1-9]", id[[1]])]
Notice that I haven't changed your regex. It could be more precise expression such as ^N\\d+$.
Do you know about strtok? It will parse your input line on certain characters. For the purpose of my example, I am breaking off a piece of my string every time I hit a space.
tempVar = strtok(string, " ");
// tempVar has "id" or everything up to the first space
while (tempVar != NULL)
{
tempVar = strtok(NULL, " ");
//now tempVar picked up the next word, and will loop picking up the next word until the end of string
}
Using this, your "Hello these are words N123456789 Hooray" would do this:
tempVar would be Hello, then "these" etc etc.
Each time through the loop tempVar would get a new value. So i would suggest evaluating tempVar in the loop (before grabbing the next one) so that you can stop when you have N123456789
Try:
gsub('\\b[a-zA-Z]+\\b','',id)

How to create string of strings in c++

I want to have sometnig like this:
first second first second
third first second first
thirst asdfasd adfads asdfadf
sdfsdf sdfasdf afdsdf dffsd
It has 4 rows and 4 columns. Each row, column pair is a string.
A string of strings is different than a table or matrix of strings.
String of Strings
Let a string be one or more sequential characters, such as "first".
A string may contain another string or commonly known as a substring. The string "theater" contains at least the strings "the", "eat", and "ate".
Matrix of Strings
A matrix of strings contains rows and columns of strings:
"first" "second" "apple" "car"
"garden" "table" "pear" "tire"
"hero" "cat" "orange" "window"
"soil" "food" "mango" "engine"
A matrix of strings can be declared as:
std::string string_matrix[4][4];
Other data structures can be used to represent a matrix of strings, such as linked lists.

Best container for string search in Qt5

In my project I need to determine occurrence of the string in the list of strings. The duplicates in the list are not allowed, and the order is irrelevant.
Please help me choose the best Qt container for the string search.
If you want a list of strings, Qt provides the QStringList class.
Once all strings are added, you can call the removeDuplicates function to satisfy your requirement of no duplicates.
To search for a string, call the filter function, which returns a list of strings containing the string, or regular expression passed to the function.
Here's an example, adapted from the Qt documentation: -
// create the list and add strings
QStringList list;
list << "Bill Murray" << "John Doe" << "Bill Clinton";
// Oops...added the same name
list << "John Doe";
// remove any duplicates
list.removeDuplicates();
// search for any strings containing "Bill"
QStringList result;
result = list.filter("Bill");
result is a QStringList containing "Bill Murray" and "Bill Clinton"
If you just want to know if a string is in the list, use the contains function
bool bFound = list.contains("Bill Murray");
Found will return true.