Let there be a sample list as ,
l = {a, {{b, c}}, {d, e}, {f}, {{g, {y}, u, h}, {{{{k, j}}}}},k, {{{{q}, {w}, {r}, {x}}}}};
ArrayRules[l]
ArrayRules::rect: Nonrectangular array encountered. >>is the error thrown. So, I was just wondering that if someone needs to preserve list structure using a similar kind of functionality, than what might be steps needed. My intention is to pass data to replace later rather than as rule in ArrayRules.
I could parse list to find positions information like ArrayRules using following code,
ListRules[l_List] :=
Module[{l1 = l, l11, s1, pos, M, tpos, trpos, sp, ntrpos, temntrpos,
fin, fin2},
{l11 = {#} & /@ l1,
s1 = Map[{#, Position[l11, #]} &, l11,Infinity] /. {a_, {{s___}}} -> {s} //. {d___, {}, e___} -> {d,e},
pos = Table[s1[[i]] //. {___, {a___List}, ___} -> a, {i, 1,Length[s1]}],
M = Last[SortBy[pos, Length]] // Length,
tpos = Table[If[Length[pos[[i]]] < M, PadRight[pos[[i]], M, 1], pos[[i]]], {i, 1, Length[pos]}],
}; tpos]
listrules= ListRules[l]
gives
{{1, 1, 1, 1, 1, 1, 1}, {2, 1, 1, 1, 1, 1, 1}, {2, 1, 1, 2, 1, 1, 1},
{3, 1, 1, 1, 1, 1, 1}, {3, 1, 2, 1, 1, 1, 1}, {4, 1, 1, 1, 1, 1, 1},
{5, 1, 1, 1, 1, 1, 1}, {5, 1, 1, 2, 1, 1, 1}, {5, 1, 1, 3, 1, 1, 1},
{5, 1, 1, 4, 1, 1, 1}, {5, 1, 2, 1, 1, 1, 1}, {6, 1, 1, 1, 1, 1, 1},
{5, 1, 2, 1, 1, 1, 2}, {5, 1, 2, 1, 1, 1, 1}, {6, 1, 1, 1, 1, 1, 1},
{7, 1, 1, 1, 1, 1, 1}, {7, 1, 1, 1, 2, 1, 1}, {7, 1, 1, 1, 3, 1, 1}, {7, 1, 1, 1, 4, 1, 1}}
Than I tried to create it back to a list using following method,
CreateList[s_List] :=
Module[{tpos = s, trpos, sp, ntrpos, temntrpos, fin, fin2, t, x, y,
h}, {trpos =
Table[tpos[[i]] -> RandomInteger[100], {i, 1, Length[tpos]}],
sp = SparseArray[trpos],
ntrpos = Normal[sp],
temntrpos = ntrpos //. {a___, {h___, 0 ..}, b___} -> {a, h, b} //. {a___, {}, b___} -> {a, b}
}; temntrpos
]
CreateList[listrules]
gives,
{{{{{{76}}}}}, {{{{{82}}, {{83}}}}}, {{{{{67}}}, {{{19}}}}}, {{{{{32}}}}}, {{{{{51}}, {{22}}, {{60}}, {{1}}}, {{{{26,59}}}}}}, {{{{{12}}}}}, {{{{{6}, {78}, {34}, {72}}}}}}
It shows that it has been able to retain nesting structure but there are extra braces associated, here I am getting defeated repeatedly. I tried to use Dimension, Depth etc. I can get rid of nesting for single elements by associating dimension from original list with them and flattening all those with maximum dimension as 1. But in case of multiple nesting, I am failing.
Can someone show me way ?
Thanks