-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract data from array of tables (C++) #89
Comments
Since there is no code I don't have any idea why your code does not work, but I can show you an example. #include <iostream>
#include "toml.hpp"
int main()
{
using namespace toml::literals;
const auto root = u8R"(
[[robot]]
name = "HelloBot"
[[robot.segment]]
name = "Roll"
type = "RotZ"
translation = [1, 2, 3]
rotation = [0.1, 0.2, 0.3]
limit = [-180, 180]
[[robot.segment]]
name = "Translation"
type = "TransZ"
translation = [0, 0, 10]
rotation = [0, 0, 0]
limit = [-40, 40]
[[robot.segment]]
name = "ElbowWrist"
type = "RotX"
translation = [4, 5, 6]
rotation = [1.1, 1.2, 1.3]
limit = [-90, 90]
)"_toml;
// loop over all the `[[robot]]` defined in a file
for(const auto& robot : toml::find<toml::array>(root, "robot"))
{
// loop over [[robot.segment]] defined in the n-th `[[robot]]`
for(const auto& segment : toml::find<toml::array>(robot, "segment"))
{
std::cout << "name = " << toml::find<std::string>(segment, "name") << std::endl;
}
}
return 0;
} The above code outputs the following. name = Roll
name = Translation
name = ElbowWrist You can also access to it by std::cout << root.at("robot").at(0).at("segment").at(0).at("name") << std::endl; // "Roll"
std::cout << root.at("robot").at(0).at("segment").at(1).at("name") << std::endl; // "Translation"
std::cout << root.at("robot").at(0).at("segment").at(2).at("name") << std::endl; // "ElbowWrist" |
And okay, the error message shown in your screenshot is confusing. I will check it out later. |
I guess you did something like the following in the second screenshot. const auto root = toml::parse("example.toml");
for(const auto& segment: toml::find(root, "robot", "segment").as_array())
{
std::cout << toml::find(segment, "name") << std::endl;
} This will say However, I have still no idea what did you do in the first screenshot. What did you do actually? Did you do this? const auto name = toml::find(root.as_array().at(0), "segment", "name"); The error message will be The confusing part of the error message shown in this case is that the underline shows the first line of the file. It wants to show the top level of the file and it shows the first line because the top-level table is defined implicitly, without a name. But the first line defines The only thing currently I can do to make it less confusing is to point the very first character of a file as the top-level table, not the whole line though I don't think this does not make it so clear.. |
@ToruNiina i think this case is worth documenting on the front page: using namespace toml::literals;
const auto raw = u8R"(
[[element]]
name = "one"
[[element]]
name = "two"
[[element]]
name = "three"
)"_toml;
const auto elements = toml::find<toml::array>(raw, "element");
const auto nelem = elements.size();
for (const auto &el : elements) {
const auto name = toml::find<std::string>(el, "name");
} I was trying to cast it initially as |
What is the best method to output the name from the first table in the array below (name = "roll")?
When using toml::find to select a robot.segment table I have encountered an error saying it is a table, and then when treating it as a table, an error that says it is an array (see screenshots).
I have also tried changing the table names to robot_segment but still no success.
[[robot]]
name = "HelloBot"
[[robot.segment]]
name = "Roll"
type = "RotZ"
translation = [1, 2, 3]
rotation = [0.1, 0.2, 0.3]
limit = [-180, 180]
[[robot.segment]]
name = "Translation"
type = "TransZ"
translation = [0, 0, 10]
rotation = [0, 0, 0]
limit = [-40, 40]
[[robot.segment]]
name = "ElbowWrist"
type = "RotX"
translation = [4, 5, 6]
rotation = [1.1, 1.2, 1.3]
limit = [-90, 90]
The text was updated successfully, but these errors were encountered: