Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

There is a sorted list of Integers.

ilist = PadRight[Sort[ RandomSample[Range[1, 99999], 3], Less], 5]  

It should become string area codes like:

{0123, 023345, 043563, 0, 0};  

with trailing zeros.
Can I do this with one function?

slist=ReplaceAll[ilist,n_Integer :>"0"<>ToString[n]]  

Note: last elements are zeros already and should not double.

share|improve this question
1  
Doe ReplaceAll[ilist, n_Integer :> "0" <> ToString[n] /; n =!= 0] work for you? –  Sjoerd C. de Vries Jun 17 '13 at 11:20
1  
slist=ReplaceAll[ilist,n_Integer/;n!=0:>"0"<>ToString[n]] aha, same to Sjoerd –  HyperGroups Jun 17 '13 at 11:22
    
these will leave 0 as an integer no?. could do ReplaceAll[ilist,n_Integer :> If[n==0,"0","0" <> ToString[n] ]] –  george2079 Jun 17 '13 at 12:44
    
If you export it as .csv you have to import it back as file.csv,"Text". . .Then your zeros are coming in to. . I like your solutions. –  Hp Radojewski Schäfer Von Jun 18 '13 at 17:35

1 Answer 1

Yet another way to do this is to PadRight with the zero string...

ilist = PadRight["0" <> ToString@ # & /@ 
          Sort[RandomSample[Range[1, 99999], 3], Less], 5, "0"]

(* {027082, 039217, 085143, 0, 0} *)

Head /@ ilist

(* {String, String, String, String, String} *)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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