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.

I am constructing a list for use in file operations. Say I want to create list which contains values from 01 to 87. The kicker is that the first nine integers need to keep a value of 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11.. etc.

So if I create list=Range[87] I generate a list of integers. Whenever I'm trying to then replace values with '01', etc. they become strings. If they are integers, they revert back to the single value. Ideally, I need a list that I can feed through Do[].

Is there a quick way to create a list that uses the two digit form the whole time?

FWIW, the specific application of this that I have a list of files i.e. {1997-01-page100.jpeg, 1997-01-page101,jpeg, <<33 379>>, 1997-87-page9.jpeg, 1997-87-page10,jpeg}. I'm using the following command to generate specific lists of the sub-file using importlist=FileNames["1997-"<>ToString[x]<>"-*.jpeg"] but am being thrown off by the zero in front of the single-digit file number.

share|improve this question

4 Answers 4

up vote 21 down vote accepted

I always use IntegerString for this (I also number my files in a similar way):

In[1]:= IntegerString[#, 10, 2] & /@ Range[87]

Out[1]= {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", \
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", \
"22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", \
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", \
"44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", \
"55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", \
"66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", \
"77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87"}

I think this is the most convenient solution, as it's built-in and doesn't require any additional effort to get it working.

share|improve this answer
    
Yup. +1 (I'm never going to catch up on points...) –  Mr.Wizard Mar 19 '12 at 13:44
    
Szabolcs's Kryptonite --- lol. –  Mr.Wizard Mar 19 '12 at 13:50
    
Oh my goodness, it is your Kryptonite -- you deleted the comment! Double lol. –  Mr.Wizard Mar 19 '12 at 20:52
    
@Mr.Wizard "I'm never going to catch up on points..." — LOL –  rm -rf Jan 13 '13 at 19:01
    
@rm-rf Marriage: the ultimate sabotage. xD –  Mr.Wizard Jan 14 '13 at 0:26

Put a zero to the left of each number, but only keep the two rightmost characters:

StringTake["0" <> ToString[#], -2] & /@ Range[87]
{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
 "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
 "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
 "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
 "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65",
 "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76",
 "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87"}
share|improve this answer

Another possibility is to use NumberForm (although the IntegerString solution is nicer in this case). Here the integer is padded with two zeroes.

Table[ToString[NumberForm[i, 2, NumberPadding -> {"0", ""}]], {i, 1, 
  10}]

{"001", "002", "003", "004", "005", "006", "007", "008", "009", "010"}

An example for adding numerical indices to a list of file names:

files = {"a.txt", "b.txt", "c.txt"};
MapIndexed[
 ToString[NumberForm[#2[[1]], 2, NumberPadding -> {"0", ""}]] <> 
   "_" <> #1 &, files]

{"001_a.txt", "002_b.txt", "003_c.txt"}

share|improve this answer

How about

StringJoin /@ Map[ToString, PadLeft[#, 2] & /@ 
  IntegerDigits[Range[87]], {2}]

Note that the result is a list of strings, but I guess you want strings to form the file names anyway.

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.