One Number Lists String to two Numbers lists

Hi All,

I have a question regarding how to manipulate strings in grasshopper, I have this string input: [78, 54, 59, 52, 25, 35, 23, 6, 17, 4, 2, 10, 16, 3, 3, 23, 30, 182, 190, 162, 107, 88, 79], [125, 133, 143, 154, 160, 177, 186, 186, 190, 194, 232, 227, 233, 235, 265, 325, 397, 333, 324, 177, 199, 195, 169],

and I want to make it into 2 lists, each containing the numbers in the [ ]
like the photo shown here
How can I do this?
Thank you!

forumQuestion.gh (7.4 KB)

Not that it’s impossible to handle the GARBAGE input format but why make it difficult?

Separate the two bracketed lists into separate text panels and avoid those brackets.


parse_text_2021_Jul18a.gh (10.9 KB)

P.S. Here is one way to handle the original GARBAGE input format but again, why make it difficult?


parse_text_2021_Jul18b.gh (9.3 KB)

A third way. Ridiculous! Avoid the garbage input in the first place.


parse_text_2021_Jul18c.gh (11.6 KB)

1 Like

There is no garbage here at all.

The simplest way to handle this type of input is with a Python script command and using eval().

No need for doing over-complicated approaches like those proposed by @Joseph_Oster .

1 Like

The input format is inherently difficult to parse in any language. Garbage. My first proposal is by far the simplest solution:

Maybe for you. The input format is simply python-style lists.

:man_facepalming:

Thank you Nathan, appreciate your answer, and your kindness

Hi Joseph, I was able to solve it as well in the end. I have this input because I was trying to send these data from pycharm, and I only know how to send one continuous string and I cannot sent 2 separate strings. Maybe because of my limited skills as well. Thank you for your solutions regardless.

What happens in Python should stay in Python.

Note that the Python solution proposed above uses two outputs instead of two branches in a data tree, as you requested. The parsing solutions I offered handle more than two lists with no problem:


parse_text_2021_Jul19a.gh (11.5 KB)

1 Like

from ghpythonlib import treehelpers as th
a = th.list_to_tree(eval(x))

mengxihex.gh (6.1 KB)

5 Likes

How would you do that in c#?
Is there a similar way?
Thanks!

private void RunScript(string x, ref object A)
{
  var re = new Regex("(?<=\\[).+?(?=\\])");
  var dt = new DataTree<int>();
  foreach(Match match in re.Matches(x))
    dt.AddRange(match.Value.Split(',').Select(i => int.Parse(i)), new GH_Path(dt.BranchCount));
  A = dt;
}

Baris.gh (6.4 KB)

2 Likes

:rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: that’s by far the most stupid thing I’ve heard since very very long time. Just admit your solution is not better than the others.
Thanks for the laugh though, you made my day!

And you just made my ignore list. There is nothing stupid about what I said. Bye-bye.

But it was @nathanletwory who set that tone first in this thread…

Thats ok. Your posts are mostly useful, but near to always come with an extreme negativeness and lack of kindness.

thanks @Mahdiyar !