27

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
canadian_scholar
  • 3,754
  • 1
  • 27
  • 45

4 Answers4

43

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
7

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"}
Brett Champion
  • 20,779
  • 2
  • 64
  • 121
6

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"}

Yves Klett
  • 15,383
  • 5
  • 57
  • 124
3

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.

Mark McClure
  • 32,469
  • 3
  • 103
  • 161