In the C programming language, arrays are defined like this:

int foo[] = {4, 8, 15, 16, 23, 42};      //Foo implicitly has a size of 6

The size of the array is inferred from the initializing elements, which in this case is 6. You can also write a C array this way, explicitly sizing it then defining each element in order:

int foo[6];        //Give the array an explicit size of 6
foo[0] = 4;
foo[1] = 8;
foo[2] = 15;
foo[3] = 16;
foo[4] = 23;
foo[5] = 42;

The challenge

You must write a program or function that expands arrays from the first way to the second. Since you are writing a program to make code longer, and you love irony, you must make your code as short as possible.

The input will be a string representing the original array, and the output will be the expanded array-definition. You can safely assume that the input will always look like this:

<type> <array_name>[] = {<int>, <int>, <int> ... };

"Type" and "array_name" will made up entirely of alphabet characters and underscores _. The elements of the list will always be a number in the range -2,147,483,648 to 2,147,483,647. Inputs in any other format do not need to be handled.

The whitespace in your output must exactly match the whitespace in the test output, although a trailing newline is allowed.

Test IO:

short array[] = {4, 3, 2, 1};

short array[4];
array[0] = 4;
array[1] = 3;
array[2] = 2;
array[3] = 1;

spam EGGS[] = {42};

spam EGGS[1];
EGGS[0] = 42;

terrible_long_type_name awful_array_name[] = {7, -8, 1337, 0, 13};

terrible_long_type_name awful_array_name[5];
awful_array_name[0] = 7;
awful_array_name[1] = -8;
awful_array_name[2] = 1337;
awful_array_name[3] = 0;
awful_array_name[4] = 13;

Submissions in any language are encouraged, but bonus points if you can do it in C.


Here is a leaderboard showing the top answers:

var QUESTION_ID=77857,OVERRIDE_USER=31716;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

  • 2
    \$\begingroup\$ Are the spaces between the array index, the equals sign, and the values required in the output? For example, would foo[0]=1; be acceptable? \$\endgroup\$
    – user45941
    Apr 15 '16 at 18:42
  • \$\begingroup\$ @Mego That would not be acceptable. The whitespace is required. I'll edit that in. \$\endgroup\$
    – DJMcMayhem
    Apr 15 '16 at 18:45
  • \$\begingroup\$ Trailing newline allowed? \$\endgroup\$
    – Luis Mendo
    Apr 15 '16 at 19:06
  • \$\begingroup\$ Are functions allowed? \$\endgroup\$
    – user45941
    Apr 15 '16 at 20:28
  • \$\begingroup\$ Is it okay to return the output instead of printing it? (in case of functions) \$\endgroup\$
    – vaultah
    Apr 15 '16 at 20:37

25 Answers 25


Vim, 54, 52, 49 47 keystrokes

2wa0<esc>qqYp<c-a>6ldf @qq@q$dT]dd:%norm dwf{xwC;<CR>gg"0P


2wa0<esc>                     'Move 2 words forward, and insert a 0.
         qq                   'Start recording in register Q
           Yp                 'Duplicate the line
             <c-a>6l          'Increment the next number then move 6 spaces right
                    df        'Delete until the next space
                       @qq@q  'Recursively call this macro

Now our buffer looks like this:

int foo[0] = {4, 8, 15, 16, 23, 42};
int foo[1] = {8, 15, 16, 23, 42};
int foo[2] = {15, 16, 23, 42};
int foo[3] = {16, 23, 42};
int foo[4] = {23, 42};
int foo[5] = {42};
int foo[6] = {42};

and our cursor is on the last line.

Second half:

$                           'Move to the end of the line
 dT]                        'Delete back until we hit a ']'
    dd                      'Delete this whole line.
      :%norm         <CR>   'Apply the following keystrokes to every line:
             dw             'Delete a word (in this case "int")
               f{x          '(f)ind the next '{', then delete it.
                  wC;       'Move a word, then (C)hange to the end of this line, 
                            'and enter a ';'

Now everything looks good, we just need to add the original array-declaration. So we do:

gg        'Move to line one
  "0P     'Print buffer '0' behind us. Buffer '0' always holds the last deleted line,
          'Which in this case is "int foo[6];"
  • 3
    \$\begingroup\$ Whenever I read a vim-golf, I realise all the coding I do (mostly keyboard commands in my miscellaneous GUI editors) looks much like this, and my mind kind of bends for a minute (Vim is just turing complete (and cooler)) :P \$\endgroup\$
    – cat
    Apr 15 '16 at 20:33
  • \$\begingroup\$ I can't get this to work - when I type the first "@q" (of "@qq@q"), the macro runs then, and apparently runs further than it should, getting things like int foo[6] = { and ending with int foo[12 (cursor on the "2") \$\endgroup\$
    – LordAro
    Apr 16 '16 at 12:08
  • \$\begingroup\$ @LordAro I probably should have mentioned that. That's because there's already a macro in q, and that runs while you're recording, messing it up. I explained how to get around that here: codegolf.stackexchange.com/a/74663/31716 \$\endgroup\$
    – DJMcMayhem
    Apr 16 '16 at 15:01
  • 1
    \$\begingroup\$ @LordAro Oh, duh, I know what's causing that. I changed df<space> to dW to save a byte, but I forgot that df<space> will break out of the macro on line 6, but dW doesn't. I'll rollback a revision. Thanks for pointing that out! \$\endgroup\$
    – DJMcMayhem
    Apr 16 '16 at 17:24
  • 1
    \$\begingroup\$ While this isn't quite the shortest answer, it's by far the most impressive. \$\endgroup\$
    – isaacg
    Apr 26 '16 at 20:17

Pyth, 44 bytes

++Khcz\]lJ:z"-?\d+"1"];"VJs[ecKd~hZ"] = "N\;

Test suite

Regular expression and string chopping. Not particularly clever.


++Khcz\]lJ:z"-?\d+"1"];"VJs[ecKd~hZ"] = "N\;
                                                Implicit: z = input()
    cz\]                                        Chop z on ']'
   h                                            Take string before the ']'
  K                                             Store it in K
 +                                              Add to that
         :z"-?\d+"1                             Find all numbers in the input
        J                                       Store them in J
       l                                        Take its length.
+                  "];"                         Add on "];" and print.
                       VJ                       For N in J:
                         s[                     Print the following, concatenated:
                            cKd                 Chop K on spaces.
                           e                    Take the last piece (array name)
                               ~hZ              The current interation number
                                  "] = "        That string
                                        N       The number from the input
                                         \;     And the trailing semicolon.
  • \$\begingroup\$ This answer is the thorn in my side. I thought I would be able to win it in vim, but for the life of me, I can't get the last 2-3 bytes off. =D Nice answer! \$\endgroup\$
    – DJMcMayhem
    Apr 16 '16 at 15:06

Retina, 108 104 100 69 bytes

Byte count assumes ISO 8859-1 encoding.

].+{((\S+ ?)+)
+`((\w+\[).+;(\S+ )*)(-?\d+).+
$1¶$2$#3] = $4;

Beat this, PowerShell...

Code explanation

First line: ].+{((\S+ ?)+)

First, we need to keep the type, array name, and opening bracket (it saves a byte), so we don't match them. So we match the closing bracket, any number of characters, and an opening curly brace: ].+{. Then we match the number list. Shortest I've been able to find so far is this: ((\S+ ?)+). We match any number of non-space characters (this includes numbers, possible negative sign, and possible comma), followed by a space, that may or may not be there: \S+ ?. This group of characters is then repeated as many times as needed: (\S+ ?)+ and put into the large capturing group. Note that we don't match the closing curly brace or semicolon. Third line explanation tells why.

Second line: $#2];$1

Since we only matched a part of input, the unmatched parts will still be there. So we put the length of the list after the unmatched opening bracket: $#2. The replace modifier # helps us with that, as it gives us the number of matches a particular capturing group made. In this case capturing group 2. Then we put a closing bracket and a semicolon, and finally our whole list.

With input short array[] = {4, 3, 2, 1};, the internal representation after this replacing is:

short array[4];4, 3, 2, 1};

(note the closing curly brace and semicolon)

Third line: +`((\w+[).+;(\S+ )*)(-?\d+).+

This is a looped section. That means it runs until no stage in the loop makes a change to the input. First we match the array name, followed by an opening bracket: (\w+\[). Then an arbitrary number of any characters and a semicolon: .+;. Then we match the list again, but this time only numbers and the comma after each number, which have a space following them: (\S+ )*. Then we capture the last number in the list: (-?\d+) and any remaining characters behind it: .+.

Fourth line: $1¶$2$#3] = $4;

We then replace it with the array name and list followed by a newline: $1¶. Next, we put the array name, followed by the length of previously matched list, without the last element (essentially list.length - 1): $2$#3. Followed by a closing bracket and assigment operator with spaces, and this followed by the last element of our number list: ] = $4;

After first replacing, the internal representation looks like this:

short array[4];4, 3, 2, 
array[3] = 1;

Note that the closing curly brace and the semicolon disappeared, thanks to the .+ at the end of third line. After three more replacings, the internal representation looks like this:

short array[4];
array[0] = 4;
array[1] = 3;
array[2] = 2;
array[3] = 1;

Since there's nothing more to match by third line, fourth doesn't replace anything and the string is returned.

TL;DR: First we change up the int list format a bit. Then we take the last element of the list and the name, and put them after the array initialization. We do this until the int list is empty. Then we give the changed code back.

Try it online!

  • \$\begingroup\$ Someone beat me to it....:( \$\endgroup\$ Apr 15 '16 at 21:01
  • \$\begingroup\$ M!` and G` are similar, but not quite the same. Be careful. \$\endgroup\$ Apr 15 '16 at 21:05
  • \$\begingroup\$ The third line explanation confuses me. The first item is the only one without a space behind it, not the last. \$\endgroup\$ Apr 17 '16 at 3:30
  • \$\begingroup\$ @CatsAreFluffy I tried changing up the wording a bit just now. What I meant was a space following a number, not preceding it. I guess I didn't fully realise the meaning of "behind". I really shouldn't write code explanations at 2 am. \$\endgroup\$
    – daavko
    Apr 17 '16 at 3:51
  • \$\begingroup\$ @daavko "Behind" normally means "after" i.e. "following", in colloquial English. You were fine. \$\endgroup\$
    – Nic
    Apr 18 '16 at 11:14

C, 215 bytes, 196 bytes

19 bytes saved thanks to @tucuxi!


char i[99],o[999],b[99],z[99];t,x,n,c;main(){gets(i);sscanf(i,"%s %[^[]s",b,z);while(sscanf(i+t,"%*[^0-9]%d%n",&x,&n)==1)sprintf(o,"%s[%d] = %d;\n",z,c++,x),t+=n;printf("%s %s[%d];\n%s",b,z,c,o);}


 *  Global strings:
 *   i: input string
 *   o: output string
 *   b: input array type
 *   z: input array name
char i[ 99 ], o[ 999 ], b[ 99 ], z[ 99 ];

/* Global ints initialized to zeros */
t, x, n, c;

    /* Grab input string from stdin, store into i */
    gets( i );

    /* Grab the <type> <array_name> and store into b and z */
    sscanf( i, "%s %[^[]s", b, z );

    /* Grab only the int values and concatenate to output string */
    while( sscanf( i + t, "%*[^0-9]%d%n", &x, &n ) == 1 )
        /* Format the string and store into a */
        sprintf( o, "%s[%d] = %d;\n", z, c++, x );

        /* Get the current location of the pointer */
        t += n;

    /* Print the <type> <array_name>[<size>]; and output string */
    printf( "%s %s[%d];\n%s", b, z, c, o );




To get the <type> <array_name>, the sscanf() format string is this:

%s          A string delimited by a space
    %[^[]   The character set that contains anything but a `[` symbol
         s  A string of that character set

To extract the int values from the string int foo[] = {4, 8, 15, 16, 23, 42};, I essentially tokenize the string with this function:

while( sscanf( i + t, "%*[^0-9]%d%n", &x, &n ) == 1 )


  • i is the input string (a char*)
  • t is the pointer location offset of i
  • x is the actual int parsed from the string
  • n is the total characters consumed, including the found digit

The sscanf() format string means this:

%*            Ignore the following, which is..
  [^0-9]      ..anything that isn't a digit
        %d    Read and store the digit found
          %n  Store the number of characters consumed

If you visualize the input string as a char array:

int foo[] = {4, 8, 15, 16, 23, 42};

with the int 4 being located at index 13, 8 at index 16, and so on, this is what the result of each run in the loop looks like:

Run 1)  String: "int foo[] = {4, 8, 15, 16, 23, 42};"
        Starting string pointer: str[ 0 ]
        Num chars consumed until after found digit: 14
        Digit that was found: 4
        Ending string pointer: str[ 14 ]

Run 2)  String: ", 8, 15, 16, 23, 42};"
        Starting string pointer: str[ 14 ]
        Num chars consumed until after found digit: 3
        Digit that was found: 8
        Ending string pointer: str[ 17 ]

Run 3)  String: ", 15, 16, 23, 42};"
        Starting string pointer: str[ 17 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 15
        Ending string pointer: str[ 21 ]

Run 4)  String: ", 16, 23, 42};"
        Starting string pointer: str[ 21 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 16
        Ending string pointer: str[ 25 ]

Run 5)  String: ", 23, 42};"
        Starting string pointer: str[ 25 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 23
        Ending string pointer: str[ 29 ]

Run 6)  String: ", 42};"
        Starting string pointer: str[ 29 ]
        Num chars consumed until after found digit: 4
        Digit that was found: 42
        Ending string pointer: str[ 33 ]
  • 1
    \$\begingroup\$ you can avoid using strcat by concatenating o inside sprintf, via %s. This should shave around 7 chars. \$\endgroup\$
    – tucuxi
    Apr 18 '16 at 10:45
  • \$\begingroup\$ @tucuxi Ah, good catch. Thanks! \$\endgroup\$ Apr 18 '16 at 18:53

V, 37 Bytes

2Eé0òYp6ldf ò$dT]ddÎdwf{xwC;

V is a 2D, string based golfing language that I wrote, designed off of vim. This works as of commit 17.


This is pretty much a direct translation of my vim answer, albeit significantly shorter.

2E                               "Move to the end of 2 words forward.
  é0                             "Insert a single '0'
    ò       ò                    "Recursively do:
     Yp6ldf                      "Yank, paste, move 6 right, delete until space.
             $dT]                "Move to the end of line, delete backwards until ']'
                 dd              "Delete this line
                   Î             "Apply the following to every line:
                    dwf{xwC;<\n> "Delete word, move to '{' and delete it, Change to end of line, and enter ';'

Then we just have:

gg"1P     "Move to line 1, and paste buffer '1' behind us.

Since this unicode madness can be hard to enter, you can create the file with this reversible hexdump:

00000000: 3245 e930 f259 7001 366c 6466 20f2 2464  2E.0.Yp.6ldf .$d
00000010: 545d 6464 ce64 7766 7b78 7743 3b0d 6767  T]dd.dwf{xwC;.gg
00000020: 2231 500a                                "1P.

This can be run by installing V and typing:

python main.py c_array.v --f=file_with_original_text.txt
  • 1
    \$\begingroup\$ Designed off of vim. 2 notes: 1. most people says from not off of, and 2. why didn't this exist. +1 \$\endgroup\$ Apr 19 '16 at 14:33

C, 195 180 bytes

195-byte original:


char*a,*b,*c,*d;j;main(i){scanf("%ms %m[^]]%m[^;]",&a,&b,&c);
for(d=c;*d++;i+=*d==44);printf("%s %s%d];\n",a,b,i);
for(d=strtok(c,"] =,{}");j<i;j++,d=strtok(0," ,}"))printf("%s%d] = %s;\n",b,j,d);}


    scanf("%ms %m[^]]%m[^;]",&a,&b,&c); // m-modifier does its own mallocs
    for(d=c;*d++;i+=*d==44);            // count commas
    printf("%s %s%d];\n",a,b,i);        // first line
    for(d=strtok(c,"] =,{}");j<i;j++,d=strtok(0," ,}"))
        printf("%s%d] = %s;\n",b,j,d);  // each array value

The two shortcuts are using the m modifier to to get scanf's %s to allocate its own memory (saves declaring char arrays), and using strtok (which is also available by default, without includes) to do the number-parsing part.

180-byte update:

char*a,*b,*c,e[999];i;main(){scanf("%ms %m[^]]%m[^}]",&a,&b,&c);
for(c=strtok(c,"] =,{}");sprintf(e,"%s%s%d] = %s;\n",e,b,i++,c),
c=strtok(0," ,"););printf("%s %s%d];\n%s",a,b,i,e);}


    scanf("%ms %m[^]]%m[^}]",&a,&b,&c);
    for(c=strtok(c,"] =,{}");sprintf(e,"%s%s%d] = %s;\n",e,b,i++,c),c=strtok(0," ,"););
    printf("%s %s%d];\n%s",a,b,i,e);

Uses bnf679's idea of appending to a string to avoid having to count commas.


Python 3.6 (pre-release), 133

m,p=str.split,print;y,u=m(input(),'[');t,n=m(y);i=m(u[5:-2],', ')
for x in range(l):p(n+f'[{x}] = {i[x]};')

Makes heavy use of f-strings.

Ungolfed version:

y, u = input().split('[')
t, n = y.split()
i = u[5:-2].split(', ')
l = len(i)
print(t, n + f'[{l}];')
for x in range(l):
    print(n + f'[{x}] = {i[x]};')
  • 1
    \$\begingroup\$ Woah, I forgot about f-strings. Those are going to be super useful for golf! \$\endgroup\$ Apr 15 '16 at 20:27
  • \$\begingroup\$ I think you can save a byte by using a list comprehension instead of the normal loop \$\endgroup\$ Apr 16 '16 at 0:30
  • \$\begingroup\$ @someonewithpc: nope, it would actually add 1 extra byte \$\endgroup\$
    – vaultah
    Apr 16 '16 at 6:35

Ruby, 127 110 108 99 88 bytes

Anonymous function with a single argument as input. Full program, reads the input from STDIN. (If you pipe a file in, the trailing newline is optional.) Returns Prints the output string.

Took @TimmyD bragging about their solution beating all other non-esolangs as a challenge, and finally overcome the (at the time of writing) 114 byte Powershell solution they had posted. Cᴏɴᴏʀ O'Bʀɪᴇɴ's trick with splitting on ] and splicing the second half to get the numbers helped.

I need to use the splat operator more. It's so useful!

Borrowed a trick from @Neil's JavaScript ES6 answer to save more bytes by scanning for words instead of using gsub and split..

t,n,*l=gets.scan /-?\w+/;i=-1
puts t+" #{n}[#{l.size}];",l.map{|e|n+"[#{i+=1}] = #{e};"}
  • \$\begingroup\$ AFAICT the task is to write a full program. \$\endgroup\$
    – vaultah
    Apr 15 '16 at 20:20
  • \$\begingroup\$ @vaultah The default for code golf is a program or function \$\endgroup\$
    – user45941
    Apr 15 '16 at 20:27
  • \$\begingroup\$ @Mego: the OP said "You must write a program" \$\endgroup\$
    – vaultah
    Apr 15 '16 at 20:27
  • \$\begingroup\$ @vaultah normally I would say that code golf allows me to use a function, but a full program saved me 2 bytes, so why not? \$\endgroup\$
    – Value Ink
    Apr 15 '16 at 20:28
  • \$\begingroup\$ Ooof ... I don't think PowerShell can get down to that. Have a +1 \$\endgroup\$ Apr 18 '16 at 12:48

05AB1E, 52 50 47 bytes


… = ¡`¦¨¨ð-',¡©gr¨s«„];«,®v¹ð¡¦¬s\¨N"] = "y';J,

Uses CP-1252 encoding. Try it online!.

  • 1
    \$\begingroup\$ It's gotten to the point where I skip all the other answers just looking for your 05AB1E answers. The language absolutely fascinates me. \$\endgroup\$ Apr 18 '16 at 19:41
  • 1
    \$\begingroup\$ @WorseDoughnut Thank you! That is the nicest thing someone has ever said to me about 05AB1E :)! \$\endgroup\$
    – Adnan
    Apr 18 '16 at 20:16

JavaScript (ES6), 100 bytes

(s,[t,n,...m]=s.match(/-?\w+/g))=>t+` ${n}[${m.length}];`+m.map((v,i)=>`
${n}[${i}] = ${v};`).join``

Since only the words are important, this works by just matching all the words in the original string, plus leading minus signs, then building the result. (I originally thought I was going to use replace but that turned out to be a red herring.)

  • \$\begingroup\$ [t,n,...m] almost a mystic vision \$\endgroup\$
    – edc65
    Apr 16 '16 at 0:01

Pyth - 53 50 46 45 44 bytes

2 bytes saved thanks to @FryAmTheEggman.


Test Suite.


Pip, 48 47 bytes

qR`(\S+)(. = ).(.+)}`{[b#Yd^k']';.n.b.,#y.c.y]}

Takes input from stdin and prints to stdout.


Tl;dr: Does a regex replacement, using capture groups and a callback function to construct the result.

The q special variable reads a line of input. The regex is (\S+)(. = ).(.+)}, which matches everything except the type (including trailing space) and the final semicolon. Using the first example from the question, the capturing groups get foo[, ] = , and 4, 8, 15, 16, 23, 42.

The replacement is the return value of the unnamed function {[b#Yd^k']';.n.b.,#y.c.y]}, which is called with the whole match plus the capturing groups as arguments. Thus, within the function, b gets capture group 1, c gets group 2, and d gets group 3.

We construct a list, the first three items of which will be "foo[", 6, and "]". To get the 6, we split d on the built-in variable k = ", ", Yank the resulting list of integers into the y variable for future use, and take the length (#). '] is a character literal.

What remains is to construct a series of strings of the form ";\nfoo[i] = x". To do so, we concatenate the following: ';, n (a built-in for newline), b (1st capture group), ,#y (equivalent to Python range(len(y))), c (2nd capture group), and y. Concatenation works itemwise on lists and ranges, so the result is a list of strings. Putting it all together, the return value of the function will be a list such as this:

["foo[" 6 "]"
 [";" n "foo[" 0 "] = " 4]
 [";" n "foo[" 1 "] = " 8]
 [";" n "foo[" 2 "] = " 15]
 [";" n "foo[" 3 "] = " 16]
 [";" n "foo[" 4 "] = " 23]
 [";" n "foo[" 5 "] = " 42]

Since this list is being used in a string Replacement, it is implicitly cast to a string. The default list-to-string conversion in Pip is concatenating all the elements:

foo[0] = 4;
foo[1] = 5;
foo[2] = 15;
foo[3] = 16;
foo[4] = 23;
foo[5] = 42"

Finally, the result (including the type and the final semicolon, which weren't matched by the regex and thus remain unchanged) is auto-printed.


Perl 5.10, 73 72 68 66 + 1 (for -n switch) = 67 bytes

perl -nE '($t,$n,@a)=/[-[\w]+/g;say"$t $n".@a."];";say$n,$i++,"] = $_;"for@a'

This is a nice challenge for Perl and the shortest among general-purpose languages so far. Equivalent to

($t, $n, @a) = /[-[\w]+/g;
say "$t $n" . @a . "];";
say $n, $i++, "] = $_;" for @a;

PowerShell v2+, 114 105 bytes

$a,$b,$c,$d=-split$args-replace'\[]';"$a $b[$(($d=-join$d|iex|iex).length)];";$d|%{"$b[$(($i++))] = $_;"}

Takes input string $args and -replaces the square bracket with nothing, then performs a -split on whitespace. We store the first bit into $a, the second bit into $b, the = into $c, and the array elements into $d. For the example below, this stores foo into $a and bar into $b, and all of the array into $d.

We then output our first line with "$a ..." and in the middle transform $d from a an array of strings of form {1,,2,,...100}; to a regular int array by -joining it together into one string, then running it through iex twice (similar to eval). We store that resultant array back in $d before calling the .length method to populate the appropriate number in between the [] in the output line.

We then send $d through a loop with |%{...}. Each iteration we output "$b..." with a counter variable $i encapsulated in brackets and the current value $_. The $i variable starts uninitialized (equivalent to $null) but the ++ will cast it to an int before the output, so it will start output at 0, all before incrementing $i for the next loop iteration.

All output lines are left on the pipeline, and output to the terminal is implicit at program termination.


PS C:\Tools\Scripts\golfing> .\expand-a-c-array.ps1 "foo bar[] = {1, 2, 3, -99, 100};"
foo bar[5];
bar[0] = 1;
bar[1] = 2;
bar[2] = 3;
bar[3] = -99;
bar[4] = 100;
  • \$\begingroup\$ Whew! I managed to get my Ruby answer golfed past yours by taking your comment about beating the other non-esolangs as a challenge! Good work, though, +1. \$\endgroup\$
    – Value Ink
    Apr 15 '16 at 20:19
  • \$\begingroup\$ @KevinLau Thanks! Back at ya with a 105 now. ;-) \$\endgroup\$ Apr 15 '16 at 20:34
  • \$\begingroup\$ I'll call your 105, and raise you a 99! :D \$\endgroup\$
    – Value Ink
    Apr 15 '16 at 21:08
  • \$\begingroup\$ No longer closing in on beating Retina. \$\endgroup\$ Apr 17 '16 at 3:33

C,278 280 bytes


x,e,l,d;char *m,*r,*a;char i[999];c(x){return isdigit(x)||x==45;}main(){gets(i);m=r=&i;while(*r++!=32);a=r;while(*++r!=93);l=r-a;d=r-m;for(;*r++;*r==44?e++:1);printf("%.*s%d];\n",d,m,e+1);r=&i;while(*r++){if(c(*r)){m=r;while(c(*++r));printf("%.*s%d] = %.*s;\n",l,a,x++,r-m,m);}}}


/* global ints
 * x = generic counter
 * e = number of elements
 * l = length of the array type
 * d = array defination upto the first '['
/* global pointers
 * m = memory pointer
 * r = memory reference / index
 * a = pointer to the start of the array type string
char *m,*r,*a;
/* data storage for stdin */
char i[999];
c(x){return isdigit(x)||x=='-';}
    while(*r++!=32);                // skip first space
    while(*++r!=93);                // skip to ']'
    for(;*r++;*r==44?e++:1);        // count elements
    printf("%.*s%d];\n",d,m,e+1);   // print array define
    while(*r++) {                   // print elements
        if(c(*r)) {                 // is char a - or a digit?
            while(c(*++r));         // count -/digit chars
            printf("%.*s%d] = %.*s;\n",l,a,x++,r-m,m);

While working on this someones posted a shorter version using sscanf for the parsing rather than using data pointers... nice one!

UPDATE: Spotted missing spaces around the equals in the element printing, IDE online link: http://ideone.com/KrgRt0 . Note this implementation does support negative numbers...


Awk, 101 bytes

{FS="[^[:alnum:]_-]+";printf"%s %s[%d];\n",$1,$2,NF-3;for(i=3;i<NF;i++)printf$2"[%d] = %d;\n",i-3,$i}

More readably:

printf "%s %s[%d];\n", $1, $2, NF - 3
for (i=3; i < NF; i++)
    printf $2"[%d] = %d;\n", i-3, $i
  • I set the field separator to everything except alphabets, digits, the underscore and -. So, the fields would be the type name, the variable name, and the numbers.
  • The number of fields will be 1 (for the type) + 1 (for the name) + N (numbers) + 1 (an empty field after the trailing };). So, the size of the array is NF - 3.
  • Then it's just printing a special line for the declaration, and looping over the numbers.
  • I should assign FS either when invoking awk (using -F) or in a BEGIN block. In the interests of brevity, ….
  • 1
    \$\begingroup\$ Actually, FS must be assigned either in BEGIN or using -F otherwise it won't be used to split the first line, and since there is only 1 line of input... \$\endgroup\$ May 8 '17 at 21:00
  • \$\begingroup\$ @RobertBenson you're right, so the command would be awk '-F[^[:alnum:]_-]+' '{printf"%s %s[%d];\n",$1,$2,NF-3;for(i=3;i<NF;i++)printf$2"[%d] = %d;\n",i-3,$i}', which is 102 bytes not counting awk itself. Hmmm. Do I get to exclude the quotes? \$\endgroup\$
    – muru
    May 11 '17 at 5:14
  • \$\begingroup\$ Yes, you can exclude quotes. You sometimes list it as C+O bytes where C and O represent the bytes from code and options respectively. Of course I generally just use a BEGIN block, so I don't have to think about it. :p \$\endgroup\$ May 11 '17 at 17:40

JavaScript ES6, 134 132 130 129 bytes

Saved 1 byte thanks to Neil.

x=>(m=x.match(/(\w+) (\w+).+{(.+)}/),m[1]+` `+(q=m[2])+`[${q.length-1}];
`+m[3].split`, `.map((t,i)=>q+`[${i}] = ${t};`).join`
  • \$\begingroup\$ Shouldn't `[${i}] = `+t+";" be `[${i}] = ${t};`? \$\endgroup\$
    – Neil
    Apr 15 '16 at 21:13
  • \$\begingroup\$ @Neil Thanks, saved a byte! \$\endgroup\$ Apr 15 '16 at 21:15

bash, 133 129 bytes

read -a l
echo "${d%?}${#e[@]}];"
for i in "${!e[@]}"
echo "${l[0]}[$i] = ${e[$i]//[!0-9]/};"

First attempt, sure its posible to get it shorter.


D, 197, 188 bytes

import std.array,std.stdio;void main(){string t,n,e;readf("%s %s] = {%s}",&t,&n,&e);auto v=e.replace(",","").split;writeln(t,' ',n,v.length,"];");foreach(i,c;v)writeln(n,i,"] = ",c,";");}

or ungolfed:

import std.array, std.stdio;

void main() {
    string type, nameAndBracket, elems;
    readf("%s %s] = {%s}", &type, &nameAndBracket, &elems);

    // remove all commas before splitting the string into substrings
    auto vector = elems.replace(",","").split();

    // writeln is shorter than fln by 1 char when filled in
    writeln(type, ' ', nameAndBracket, vector.length, "];");

    // print each element being assigned
    foreach(index, content; vector)
        writeln(nameAndBraket, index, "] = ", content, ";");
  • \$\begingroup\$ I don't know D, but could you read the opening square bracket as part of the name? That would save you having to write a square bracket separately later. \$\endgroup\$
    – DLosc
    Apr 17 '16 at 21:09

Julia, 154 134 101 bytes

f(s,c=matchall(r"-?\w+",s),n=endof(c)-2)=c[]" "c[2]"[$n];
"join([c[2]"[$i] = "c[i+3]";
"for i=0:n-1])

This is a function that accepts a string and returns a string with a single trailing newline.


function f(s, c = matchall(r"-?\w+", s), n = endof(c) - 2)
    c[] " " c[2] "[$n];\n" join([c[2] "[$i] = " x[i+3] ";\n" for i = 0:n-1])

We define c to be an array of matches of the input on the regular expression -?\w+. It caputres the type, array name, then each value. We store n as the length of c - 2, which is the number of values. The output is constructed as the type, name and length string interpolated, combined with each definition line separated by newlines. For whatever reason, c[] is the same as c[1].

Saved 32 bytes with help from Dennis!


Python 2, 159 bytes

print'%s %s[%d];'%(t,n,len(a))
for i in range(len(a)):print'%s[%d] = %s;'%(n,i,a[i])

Try it online

Thanks Kevin Lau for some golfing suggestions


Python 3, 116 bytes

for x in l:print(v%i,'= %s;'%x.strip('{,};'));i+=1

Splits the input into the type, name, and the list of numbers. After printing the array declaring, prints the elements by manually enumerating through the numbers, removing excess punctuation that attached to the first and last one.

A different approach in Python 2 came out to 122 bytes:

a,b=input()[:-2].split('] = {')
print a+`len(l)`+"];"
for y in enumerate(l):print a.split()[1]+'%s] = %s;'%y

The idea is to eval the list of numbers as a tuple, with a comma at the end so that a single number is recognized as a type. The enumerated list of numbers provides tuples to string-format in.


PHP, 143 bytes


<?$t=count($n=explode(' ',preg_replace('/[^\s\w]/','',$argv[1])))-3;echo"$n[0] {$n[1]}[$t];";for($i=2;$t>$j=++$i-3;)echo$n[1]."[$j] = $n[$i];";


$t = count(                                  // Get the number of elements for our array...
    $n = explode(' ',                            // After split the input on whitespace...
    preg_replace('/[^\s\w]/','',$argv[1])))-3;  // After removing all special characters.
echo "$n[0] {$n[1]}[$t];";                     // First line is type, name, and count.
for($i=2;                                        // Loop through array elements
    $t > $j = ++$i-3;)                         // Assign j to be the actual index for our new array
    echo $n[1]."[$j] = $n[$i];";                // Print each line

Input is taken through command line argument. Sample:

C:\(filepath)>php Expand.php "int foo[] = {4,8,15,16,23,42};"


int foo[6];foo[0] = 4;foo[1] = 8;foo[2] = 15;foo[3] = 16;foo[4] = 23;foo[5] = 42;

MATL, 68 64 58 bytes

'\w+'XX2:H#)XKxXIZc'['KnV'];'v!K"I2X)'['X@qV'] = '@g';'6$h

This isn't C, but it does use C-like sprintf function Nah, that was wasting 4 bytes.

Try it online!

          % Take input implicitly
'\w+'XX   % Find substrings that match regex '\w+'. Gives a cell array
2:H#)     % Split into a subarray with the first two substrings (type and name), and 
          % another with the rest (numbers that form the array)
XKx       % Copy the latter (numbers) into clipboard K. Delete it
XI        % Copy the former (type and name) into clipboard I
Zc        % Join the first two substrings with a space
'['       % Push this string
K         % Paste array of numbers
nV        % Get its length. Convert to string
'];'      % Push this string
v!        % Concatenate all strings up to now. Gives first line of the output
K"        % For each number in the array
  I2X)    %   Get name of array as a string
  '['     %   Push this string
  X@qV    %   Current iteration index, starting at 0, as a string
  '] = '  %   Push this string
  @g      %   Current number of the array, as a string
  ';'     %   Push this string
  5$h     %   Concatenate top 6 strings. This is a line of the output
          % Implicity end for each
          % Implicitly display

Clojure, 115 bytes

#(let[[t n & v](re-seq #"-?\w+"%)](apply str t" "n\[(count v)"];\n"(map(fn[i v](str n"["i"] = "v";\n"))(range)v))))

I wasn't able to nicely merge awful_array_name[5]; and awful_array_name[0] = 7; parts so that they'd re-use code :/


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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