27
$\begingroup$

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.

$\endgroup$

4 Answers 4

43
$\begingroup$

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.

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

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"}
$\endgroup$
6
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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