Re: Parsing awkward text in a nested list
- To: mathgroup at smc.vnet.net
- Subject: [mg107757] Re: Parsing awkward text in a nested list
- From: Raffy <adraffy at gmail.com>
- Date: Thu, 25 Feb 2010 07:45:37 -0500 (EST)
- References: <hm56mc$k69$1@smc.vnet.net>
On Feb 24, 10:51 pm, Garapata <warsaw95... at mypacks.net> wrote:
> I have a nested list with the following dimensions: {3,2,2}
>
> It looks something like this:
>
> list1 =
> {
> {{"item1", "{ text, text, text}"}, {"item2", 24}},
> {{"item1", "{ text, text, text}"}, {"item2", 24}},
> {{"item1", "{ text, text, text}"}, {"item2", 24}}
>
> }
>
> I want to get the following:
>
> list2 =
> {
> {{"item1", {"text", "text", "text"}}, {"item2", 24}},
> {{"item1", {"text", "text", "text"}}, {"item2", 24}},
> {{"item1", {"text", "text", "text"}}, {"item2", 24}}
>
> }
>
> I've come up with a very cumbersome way to do it:
>
> Diagonal[ReplacePart[list1,{#,1,2}->StringSplit[StringTrim[StringTrim[ li=
st1[[All,1,2]],("{"|"}")]],","]
>
> [[#]]]&/@Range[Length[list1]]]
>
> Can anyone suggest a simpler or more elegant solution.
>
> Thx.
Given that the proper way to parse " {a , b ,c } " is {"a", "b",
"c"} using the following rules:
1. StringTrim[" {a , b ,c } "] = "{a , b ,c }"
2. StringTake["{a , b ,c }", {2, -2}] = "a , b ,c "
3. StringSplit["a , b ,c ", ","] = {"a ", " b ", "c "}
4. StringTrim[{"a ", " b ", "c "}] = {"a", "b", "c"}
I'd recommend:
list2 = list1;
list2[[All, 1, 2]] =
StringTrim@StringSplit[StringTake[StringTrim[#], {2, -2}], ","] & /@
list2[[All, 1, 2]];