{
"80550560": {"name":" HAdailton Cesar", "name2": "T-Max"},
"5987810": {"name": "Adnax", "name2": "Adna Zaza"}
}
I have this input and I need to output all the names that comes in the input, but the problem is that i don't have integer organized index, I would have to get the string number and also I don't know what the string text index is going to be.
I would imagine something like this, but I don't know how to get the 'string_text' from JsonCPP
res[string_text]["name"];
Use getMemberNames to get a list.
I'm pretty sure it is possible to iterate through too, but I have always opted to use `getMemberNames'
Reading the documentation for the Json::Value class, it have iterator capabilities like begin and end, so it should be possible to iterate the values like a standard container.
Related
I have generated a list from file.
The list contains a list that looks this below
(redTomatoes, 47.50)
(greenapple, 23.75)
(yellowBananas, 17)
and so forth
var value = versionsList.getValue("banana")
println("This is the value $value")
The above code won't retrun the value because it doesn't match exactly. I can't add ignoreCase or contains with .getValue
Is it possible to search for a String containing a word while ignoring case, spacing?
Do I need to create some sort of loop to test all possible combinations to get value?
Thanks
Is it possible to search for a String containing a word while ignoring case, spacing?
It is possible, but it is not something that can be done without iterating over all elements.
Do I need to create some sort of loop to test all possible combinations to get value?
So yes, a loop of some sort is needed, but testing all possible combinations is a bit extreme. A preferable way would have been to use .toLowerCase() on any String before putting it into the map, even tough it would not entirely remove the ambiguity of the format of the keys. But even without such altering of the input, it can be solved.
A Proposed Solution
Given the input such as
val fruits: Map<String, Double> = mapOf(
"redTomatoes" to 47.50,
"greenapple" to 23.75,
"yellowBananas" to 17.0
)
and the assumption that the data cannot be modified before inserting to the map, I propose that we convert them into a more common format when retrieving a value from the map. Since the irregularities is capital letters and spaces between words, we need to transform all keys and inputs to a common format without spaces and in lower case. Transforming a String can conveniently be written as an extension function.
fun String.toLowerCaseWithoutWhiteSpace() = this.toLowerCase().replace(Regex("\\s+"), "")
But as we cannot use .getValue() on our map for this, we need to write a custom function for that.
fun <T> Map<String, T>.getLike(key: String): T?
{
val transformed = key.toLowerCaseWithoutWhiteSpace()
return this
.asSequence()
// Change the keys in the map so they are also lower case and without whitespace
.map { it.key.toLowerCaseWithoutWhiteSpace() to it.value}
// Take the first entry where the key matches
.firstOrNull { (keyInMap, _) ->
keyInMap == transformed
}
// Return the value from the entry
?.second
}
And we can then use the following to access the right values
fruits.getLike("yellow bananas")
fruits.getLike("green apple")
fruits.getLike("red tomatoes")
This does of course come with a performance penalty, accessing the right value will have a time complexity of O(N) instead of O(1). This might be okay for a smaller map, but if there will be a lot of elements in your map, you might consider processing the elements before inserting them in the map, or if it is not possible, creating a new map were all keys have been converted as I have done above.
Let's say I have the following strings:
"This [color=RGB]is[\color] a string."
"This [color=RGB][bold]is[\bold][\color] another string."
What I'm looking for is a good way to parse the string in order to extract the tag information and then reconstruct the original string without tags.
The tag informations will be used during text rendering.
Obviously I can achieve the goal by working directly with strings (find/substr/replace and so on), but I'm asking if there is another way cleaner, for example using regular expression.
Note:
There are very few tags I need, but there is the possibility to nest them (only of different type).
Can't use Boost.
There's a very simple answer that might work, depending on the complexity of your strings. (And me understanding you correctly, i.e. you just want to get the cleaned up strings, not actually extract the tags.) Just remove all tags. Replace
\[.*?]
with nothing. Example here
Now, if your string should be able to contain tag-like objects this might not work.
Regards
Here is my function that parses an addition equation.
expr_print({num,X}) -> X;
expr_print({plus,X,Y})->
lists:append("(",expr_print(X),"+",expr_print(Y),")").
Once executed in terminal it should look like this (but it doesn't at the moment):
>math_erlang: expr_print({plus,{num,5},{num,7}}).
>(5+7)
Actually one could do that, but it would not work the way wish in X in {num, X} is a number and not string representation of number.
Strings in Erlang are just lists of numbers. And if those numbers are in wright range they can be printed as string. You should be able to find detail explenation here. So first thing you wold like to do is to make sure that call to expr_print({num, 3}). will return "3" and not 3. You should be able to find solution here.
Second thing is lists:append which takes only one argument, list of list. So your code could look like this
expra_print({num,X}) ->
lists:flatten(io_lib:format("~p", [X]));
expr_print({plus,X,Y})->
lists:append(["(", expr_print(X),"+",expr_print(Y), ")"]).
And this should produce you nice flat string/list.
Another thing is that you might not need flat list. If you planning to writing this to file, or sending over TCP you might want to use iolist, which are much easier to create (you could drop append and flatten calls) and faster.
I am making a simple program where if you type 'north' it will subtract a number from your stamina. I got that too work, but there are a lot of possible input to represent 'north' you can have 'no', 'n','NORTH','NoRtH' as you can see this gets tedious after a while, if I have to create an if statement for each and every single possible variation of the word north, and not to mention the other directions I want to use, it won't look nice
I was wondering is there a way to be able to store all those possible variations of that word into something, and when when I make an if statement, it directs it to that storage unit to compare with all possible variations... because I just know that if i make 40+ if and else if statements to compare the users input with all those variations, the code will get ugly and fast.
It sounds like you want to see if a string is contained within an arbitrary set of strings. The C++ standard libraries have functionality for this.
I would use a std::set to contain a list of valid terms, like so:
std::set<std::string> north_terms_set;
north_terms_set.insert("n");
north_terms_set.insert("north");
and then check to see if a given input belongs to the set as follows:
//Convert input to lowercase
std::string input_str = "NoRtH";
std::transform(input_str.begin(), input_str.end(), input_str.begin(), ::tolower);
//compare lowercase version of input to the set of valid terms.
if (north_terms_set.find(input_str) != north_terms_set.end()) {
//User has typed something like 'north', subtract from stamina, etc.
}
While the method suggested by 111111 is simpler, this methods enables you to limit which variants are considered valid and also lets you allow alternatives such as 'north', 'up', 'top', etc.
If you want to match the following to north:
n, no, nor, nort, north
including their capitalised counter parts you can do this if you use boost string algorithms, you can obviously write your own to_lower and starts_with if you can't use boost for some reason.
std::cin >> input;
boost::to_lower(input);
if(boost::starts_with("north", input)) {
//match
}
This is a very simple thing, so I want to keep it as simple as it sounds. All I want is to load a bunch of key-value paires from a file, and populate them in to a map. I do not really care how the text is structured, as long as it is easy to read.
What i have now is:
xml with xsd generated code (overkill)
Protocol buffer (also overkill)
INI style text file
I like the syntax of the INI file, but I not want to write a parser for that. It sounds to me like I would be doing something lots of people have done before me. Is there not some sort of library to read simple structured files like this?
Since you seem to want the simplest thing humanly possible, I'm going to suggest something incredibly simple that may or may not work based on your map contents. If your map data values are strings that include spaces, this wont work. If they're strings without spaces, or numeric, you're set.
This isn't tested code, but it's close and simple so you should be fine even if it doesn't quite compile. Just change KeyType and ValueType to int, string, float, or whatever you're actually using in the file.
Set up file like:
key value
key2 value2
key3 value3
key4 value4
Read like:
KeyType key;
ValueType value;
std::map<KeyType, ValueType> myMap;
while (infile >> key >> value)
myMap[key] = value;
If you are in the MS world you can use
GetPrivateProfileSectionNames
GetPrivateProfileString
WritePrivateProfileString
to read from ini file or regestry. If you want to write Unicode make sure a newly created file gets the BOM of UTF16.