Visual C++ is there something like explode for Strings? - visual-c++

I have a CString that I'd like to break up much the way a php explode() would do, I haven't seen anything like this in C++. Does anybody have an easy way to split up a string into substrings given the separator character?

There's no direct equivalent, but the Tokenize method is similar, and can be used as the basis of function to work like PHP's explode.

Related

Ansible: Any way to do a non-lexicographic sorting of strings containing integers?

I have multiple versions of scripts files within the same directory. I want ansible via the find: module, through some sort of sorting comparison to always choose the highest version file it can. The issue is that the integer sorting I desire won't work because the strings in question aren't a pure integer comparison, and the lexicographic sorting won't give me the proper expected version. These filenames have a naming "convention" but there is no exact hard-coded filename versioning to work with, the versions are random within each project, but if integer ordering could be used, I/we could determine which file to use for each task.
Example, lets say I have the following 3 strings:
script_v3.sh
script_v9.sh
script_v10.sh
The normal sorting methods/filters within ansible (such as the sort filter) will try to do a lexicographic comparison of these strings in order. This means the one with the "highest" value will be script_v9.sh which is bad as I/we expect it should be script_v10.sh this is no good, as it will try to use script_v9.sh and then cause the rest of that process/task to fail. I would love to turn them into integers to do an integer comparison/sort, but as there are other non-numerical characters in the string every attempt so far to do so has been a failure. Note that I/we also need to occasionally use the lowest version in some tasks as well, which also screws up utilizing our example if lexicographic sorting is used.
I would like to know if this is possible to accomplish through some convenient comparison method, or filter which I have overlooked, or if anyone has any better ideas? The only thing I could possibly come up with is to use a regex to strip out the integers, compare them by themselves as integers, and then finally match up the result to the filename which contains the highest 10 value and then have the task use that. However, I'm horrible at regular expressions, and I'm not even certain that's the most elegant way to approach this. Anyone who could help me out would be highly appreciated.

Efficient way for multiple string pattern matching?

Suppose now we have:
String[] = {
"Name:John, State:MA, City:Boston, Degree:Master",
"Name:Alex, State:CA, City:San Diego, Degree:PhD",
"Name:Aaron, State:NY, City:NYC, Degree:Master",
"Name:Lily, State:MA, City:Worcester, Degree:Master",
}
How I'd like to find ALL that contain both "State:MA" and "Degree:Master"; so obviously that'll be line 1 and 4.
So it looks like SQL database query but I need to implemented using Java or Python.
Also, the input data is supposed be very big, so I'm actually considering more efficient ways like Trie to store the information.
But usually Trie is supposed for prefix string question; say, given a list of strings we'd like to find all strings that contain pattern he,
so final list could be like:
he, hell, help, hello....
While for my question, the two patterns they are not continuous together; but Trie indeed can save lots of space for big input.
So any ideas to solve such multiple pattern matching using Trie?
Or other data structures I don't know?
Thanks
For inspiration you may look at these classes. You'd better start with samples first. The approach is kind of hybrid of a trie and FSA.
You'll have to implement the logic for preparing patterns on your own. Also you'd have to take care of the order of results when multiple patterns match your string.

Convert string with exponential number to floating

I'm working on a NodeJs script that handles strings with exponential values.
Something like this:
1.070000000000000e+003
Which is the best way to convert (or parse) this string and obtain a floating value?
Thanks for the help.
You may convert by using parseFloat or Number
If you prefer to parse, maybe the best way is by a regular expression:
/-?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/
as suggested here and convert the single capturing group.

Converting First order logic to CNF

Is there a existing implementation in C/c++/java to convert first order logic to CNF ?
It might be more efficient to use something like: Boolean Normal Form
For implementation, I recommend doing it yourself for something simple like this. An efficient method has psuedo code + explanation here

how to write read readable strings in scala

In scala, when you write out a string "Hello World" to a file it writes
Hello World
(note: no double quotes).
Lisp has a concept of print and write. One writes without the double quotes, the other includes them to make it easy to write out data structures and read them back later using the standard reader.
Is there anyway to do this in Scala?
With one string it is easy enough to format it - but with many deeply nested structures, it is nearly impossible.
For example, say I have
sealed trait PathSegment
case class P(x:String) extends PathSegment
case class V(x:Int) extends PathSegment
To create one does:
P("X")
or
V(0)
a list of these PathSegments prints as:
List(P(paths), P(/pets), P(get), P(responses), V(200))
I want this to print out as:
List(P("paths"), P("/pets"), P("get"), P("responses"), V(200))
In other words, I want strings (and characters), no matter where to occur in a structure to print out as "foo" or 'c'
That's what Serialization is about. Also, why JSON is popular.
Check out lift-json ( https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/ ) for writing data out that will be parsed and read by another language. JSON is pretty standard in the web services world for request/response serialization and there are JSON libraries in just about every language.
To literally write out a string including double quotes, you can also do something like this:
"""
The word "apple" is in double quotes.
"""
I find a slightly more structured format like JSON more useful, and a library like lift-json does the right thing in terms of quoting Strings and not quoting Ints, etc.
I think you are looking for something like Javascript's eval() + JSON, and Python's eval(), str() and repr(). Essentially, you want Lispy symmetric meta-circular evaluation. Meaning you can transform data into source code, and evaluating that source code with give you back the same data, right?
AFAIK, there's no equivalent of eval() in Scala. Daniel Spiewak has talked about this here before. However, if you reeeeeealy want to. I suggest the following things:
Every collection object has 3 methods that will allow you to transform its data to a string representation anyway you want. There are mkString, addString and stringPrefix. Do something clever with them (think "decompiling" your in-memory ADTs back to source-code form) and you shall arrive to step 2). Essentially, you can transform a list of integers created by List(1,2,3) back to a string "List(1,2,3)". For more basic literals like a simple string or integer, you'll need to pimp the built-in types using implicits to provide them with these toString (I'm overloading the term here) helper methods.
Now you have your string representation, you can think about how to "interpret" or "evaluate" them. You will need an eval() function that create a new instance of a parser combinator that understands Scala's literals and reassemble the data structure for you.
Implementing this actually sounds fun. Don't forget to post back here if you've successfully implementing it. :)

Resources