Skip to content
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

how to get length of nested array? and extract all son keys? #33

Open
xkungfu opened this issue Jan 12, 2022 · 6 comments
Open

how to get length of nested array? and extract all son keys? #33

xkungfu opened this issue Jan 12, 2022 · 6 comments

Comments

@xkungfu
Copy link

xkungfu commented Jan 12, 2022

yaml 1:

'list':
  '2021-10-08':
    - code: '300209'
      focusstr: ''
    - code: '300047'
      focusstr: ''
    - code: '300970'
      focusstr: ''
    - code: '300288'
      focusstr: ''
  '2021-10-11':
    - code: '300162'
      focusstr: ''
    - code: '300209'
      focusstr: ''
    - code: '300972'
      focusstr: ''
    - code: '300159'
      focusstr: ''

yaml2:

'2021-10-08':
  - code: '300209'
    focusstr: ''
  - code: '300047'
    focusstr: ''
  - code: '300970'
    focusstr: ''
  - code: '300288'
    focusstr: ''
'2021-10-11':
  - code: '300162'
    focusstr: ''
  - code: '300209'
    focusstr: ''
  - code: '300972'
    focusstr: ''
  - code: '300159'
    focusstr: ''

want to get how many date string in the yaml, and extract all the date strings to a array such as vector.
so I can loop the yaml by date keys.

thanks!

@sea-kg
Copy link
Member

sea-kg commented Jan 12, 2022

Try this:

For yaml1:

WsjcppYaml yaml;
std::vector<std::string> vKeys = yaml["list"].keys();

For yaml2:

WsjcppYaml yaml;
std::vector<std::string> vKeys = yaml.getCursor().keys();

More samples

std::string sTest =     
        "'2021-10-08':\n"
        "  - code: '300209'\n"
        "    focusstr: ''\n"
        "  - code: '300047'\n"
        "    focusstr: ''\n"
        "  - code: '300970'\n"
        "    focusstr: ''\n"
        "  - code: '300288'\n"
        "    focusstr: ''\n"
        "'2021-10-11':\n"
        "  - code: '300162'\n"
        "    focusstr: ''\n"
        "  - code: '300209'\n"
        "    focusstr: ''\n"
        "  - code: '300972'\n"
        "    focusstr: ''\n"
        "  - code: '300159'\n"
        "    focusstr: ''\n"
    ;

    WsjcppYaml yaml2;
    if (!yaml2.loadFromString("test", sTest, sError)) {
        return -1;
    }
    WsjcppYamlCursor cur = yaml2.getCursor();
    std::vector<std::string> vKeys = cur.keys();
    for (int i = 0; i < vKeys.size(); i++) {
        std::string sKey = vKeys[i];
        int nCount = cur[sKey].size(); // musts array
        for (int n = 0; n < nCount; n++) {
            WsjcppYamlCursor el = cur[sKey][n];
            std::cout
                << "code = " << el["code"].valInt()
                << "; focusstr = " << el["focusstr"].valStr()
                << ";"
                << std::endl;
        }
    }

output:

code = 300209; focusstr = ;
code = 300047; focusstr = ;
code = 300970; focusstr = ;
code = 300288; focusstr = ;
code = 300162; focusstr = ;
code = 300209; focusstr = ;
code = 300972; focusstr = ;
code = 300159; focusstr = ;

@xkungfu
Copy link
Author

xkungfu commented Jan 12, 2022

I will try and post codes tomorrow.
thank you very much!

@sea-kg
Copy link
Member

sea-kg commented Jan 12, 2022

You welcome!

@xkungfu
Copy link
Author

xkungfu commented Jan 13, 2022

WsjcppYamlCursor el = cur[sKey][n];

I just tried to write a loop for almost one hour and haven't got a right output, then find your code.
I am a newbie.
thanks for your detailed guide step by step!

@xkungfu
Copy link
Author

xkungfu commented Jan 13, 2022

one more question:
python yaml generate the format without indent, or indent after "-", like below:

with open(path, 'w') as f:
 yaml.dump(a_dict, f,  default_flow_style=False, sort_keys=False)

generate result like:
std::string sTest =     
        "'2021-10-08':\n"
        "-code: '300209'\n"
        " focusstr: ''\n"
        "-code: '300047'\n"
        " focusstr: ''\n"
        "-code: '300970'\n"
        " focusstr: ''\n"
        "-code: '300288'\n"
        " focusstr: ''\n"
        "'2021-10-11':\n"
        "-code: '300162'\n"
        " focusstr: ''\n"
        "-code: '300209'\n"
        " focusstr: ''\n"
        "-code: '300972'\n"
        " focusstr: ''\n"
        "-code: '300159'\n"
        " focusstr: ''\n"
    ;

with open(path, 'w') as f:
 yaml.dump(a_dict, f, indent=4, default_flow_style=False, sort_keys=False)

generate result like:
std::string sTest =     
        "'2021-10-08':\n"
        "-    code: '300209'\n"
        "     focusstr: ''\n"
        "-    code: '300047'\n"
        "     focusstr: ''\n"
        "-    code: '300970'\n"
        "     focusstr: ''\n"
        "-    code: '300288'\n"
        "     focusstr: ''\n"
        "'2021-10-11':\n"
        "-    code: '300162'\n"
        "     focusstr: ''\n"
        "-    code: '300209'\n"
        "     focusstr: ''\n"
        "-    code: '300972'\n"
        "     focusstr: ''\n"
        "-    code: '300159'\n"
        "     focusstr: ''\n"
    ;

It seems with indent or without indent are all valid, but wsjcpp-yaml need indent at the line start of array elements.

@sea-kg
Copy link
Member

sea-kg commented Nov 25, 2022

@xkungfu sorry for a longtime.... so... python can parse this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants