Search code examples
maple

MAPLE, How to convert a string (with my structure) to a set?


i have a text file with 3 lines:

B = 2*Pi/n, true, {B, n}

a = 2*R*sin(B/2), true, {a, R, B}

P = n*a, true, {P, n, a}

i want to read this file in maple. Each line will be a set like this

set1 := {B = 2*Pi/n, true, {B, n}}

set2 := {a = 2*R*sin(B/2), true, {a, R, B}}

set3 := {P = n*a, true, {P, n, a}}

i tried to read the text file line by line (using readline ) and i got 3 string.

str1 := "B = 2*Pi/n, true, {B, n}"

str2 := "a = 2*R*sin(B/2), true, {a, R, B}"

str3 := "P = n*a, true, {P, n, a}" 

Is there any sort way to convert these strings to sets?


Solution

  • You can use cat to add braces to the strings and then parse them:

    for i to 3 do
        (set || i) := parse(cat("{", (str || i), "}"));
    end do;
    

    If you have any control over the input file, you could do this a little more easily with the read command and input that is valid Maple commands:

    set1 := {B = 2*Pi/n, true, {B, n}};
    

    e.g.