Timeline for Parsing a string with named sections and a key-value pair on each line
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 23, 2017 at 12:41 | history | edited | CommunityBot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
May 1, 2016 at 21:40 | comment | added | BrainFRZ |
Again, we'd need to know from the OP, but his sample text suggests that there's a blank line between each block. Bottom line, since I'm getting the "extended discussion" thing, his code can be vastly improved no matter what to not need the continue s and nested if-checks, and my version also tells you exactly what's going on where with variable names instead of unnamed substrings. It's also separated the parsing of data from the assignment to the metrics and result objects, where both yours and his have assignments in two places.
|
|
May 1, 2016 at 21:32 | comment | added | Tunaki |
@BrainFRZ Yes all empty lines are skipped but the fact that there is an empty line is not the trigger for the start of a new group. The new group starts when cut = s.indexOf('-'); is different then -1 . If you run the code in the question on the example data but without empty lines, the result is still the same.
|
|
May 1, 2016 at 21:16 | comment | added | BrainFRZ |
We'd need a confirmation from @user1950349 but his code had if (s.trim().isEmpty()) { continue; // Skip empty line } and his example text had a blank line between each line
|
|
May 1, 2016 at 20:46 | comment | added | Tunaki |
I don't think the original code assumes an empty line before each group. A new group is only started when a line containing a '-' is detected, whatever is before (I tested without the empty line and it still works).
|
|
May 1, 2016 at 20:43 | comment | added | BrainFRZ |
Sure, but so did his code, and so did his example. In order for any parsing to be possible, we have to know how may lines there are, or there has to be some unique delimiter we can use. Whatever the delimiter is, we can take advantage of it either with .isEmpty() for a blank line or .equals(delim) if it's something else for some reason :)
|
|
May 1, 2016 at 20:41 | comment | added | Tunaki | Now it assumes that there is an empty line before each new group :) | |
May 1, 2016 at 20:37 | comment | added | BrainFRZ | I've updated my answer to handle a variable number of keys instead | |
May 1, 2016 at 20:37 | history | edited | BrainFRZ | CC BY-SA 3.0 |
Updated for variable number of keys
|
May 1, 2016 at 20:25 | comment | added | user1950349 |
I don't know what keys might be beforehand so it has to be dynamic. In general depending on response variable, it should load key:value pairs in a map, no matter how many are there.
|
|
May 1, 2016 at 20:24 | comment | added | BrainFRZ | That's true. As long as there are always the same number of values though, it's an easy update. We'd need more info from the OP to know if it's variable or not. | |
May 1, 2016 at 20:23 | comment | added | user1950349 |
In actual example, it can have more than two values that I need to put in the map. Load and Peak was just an example, it can have more key:value pairs.
|
|
May 1, 2016 at 20:21 | comment | added | Tunaki | Note that this assumes that there are only 2 values to put in the map. | |
May 1, 2016 at 20:18 | history | answered | BrainFRZ | CC BY-SA 3.0 |