-5
$\begingroup$

This question already has an answer here:

I have nested-lists in number format,like

 data={{1,2,3},{2,{3,4,50}},{4,5,6},{3,{3,{5,6}}}}

I want to convert each number into String format.For that purpose I tried the following code,but it doesn't working.

case1 :

 stringData=ReplaceAll[data, Identity -> String]

case2:

stringData= MapAll[ToString, data]

Testcase 1:

Part[stringData, 1, 2] + 10

I evaluated testcase,but it showing 12.

How can I solve this?

feel free,If you want to edit my question.

$\endgroup$

marked as duplicate by Oleksandr R., Jens, Sjoerd C. de Vries, Yves Klett, m_goldberg Mar 15 '13 at 13:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Look at the docs for ToString. $\endgroup$ – whuber Mar 14 '13 at 16:25
3
$\begingroup$

Building on your first try, an alternative to acl's approach is

l = {{1, 2, 3}, {2, {3, 4, 50}}, {4, 5, 6}, {3, {3, {5, 6}}}};
ReplaceAll[l, n_Integer :> ToString[n]]

which gives

{{"1", "2", "3"}, {"2", {"3", "4", "50"}}, {"4", "5", "6"}, {"3", {"3", {"5", "6"}}}}
$\endgroup$
2
$\begingroup$

Does Map like this

Map[
 ToString[#] &,
 {{1, 2, 3}, {2, {3, 4, 50}}, {4, 5, 6}, {3, {3, {5, 6}}}},
 {-1}
 ]

do what you want?

$\endgroup$
  • 2
    $\begingroup$ Why do you use ToString[#] &, instead of just ToString? $\endgroup$ – Sjoerd C. de Vries Mar 14 '13 at 16:35
  • 1
    $\begingroup$ @SjoerdC.deVries to annoy MrW with my lack of brevity :) $\endgroup$ – acl Mar 14 '13 at 16:38
  • $\begingroup$ @acl Ah, then why not Function[string, ToString[string]][#]& :P $\endgroup$ – rm -rf Mar 14 '13 at 17:16
  • $\begingroup$ @rm-rf He ought then to prefer ToString[#] & @# &@# &@# &@# &@# &@# &@# &@# &@# &. Oh, sorry, that's too compact: how about ToString[#] &[#] &[#] &[#] &[#] &[#] &[#] &[#] &[#] &[#] &[#] & instead? :-) $\endgroup$ – whuber Mar 14 '13 at 17:21
  • 1
    $\begingroup$ Oh gads, I've been sniped. I was about to comment on the unnecessary Function in ToString[#] & and then I saw the comments. I hate you for the next 13.7 minutes. ;-p $\endgroup$ – Mr.Wizard Mar 14 '13 at 18:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.