[Question]
  [
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:
```
#in
short array[] = {4, 3, 2, 1};
#out
short array[4];
array[0] = 4;
array[1] = 3;
array[2] = 2;
array[3] = 1;
#in
spam EGGS[] = {42};
#out
spam EGGS[1];
EGGS[0] = 42;
#in
terrible_long_type_name awful_array_name[] = {7, -8, 1337, 0, 13};
#out
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.
# Leaderboard:
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>
```
  
[Answer]
# Vim, ~~54, 52, 49~~ 47 keystrokes
---
```
2wa0<esc>qqYp<c-a>6ldf @qq@q$dT]dd:%norm dwf{xwC;<CR>gg"0P
```
Explanation:
```
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];"
```
[Answer]
# Pyth, 44 bytes
```
++Khcz\]lJ:z"-?\d+"1"];"VJs[ecKd~hZ"] = "N\;
```
[Test suite](https://pyth.herokuapp.com/?code=%2B%2BKhcz%5C%5DlJ%3Az%22-%3F%5Cd%2B%221%22%5D%3B%22VJs%5BecKd~hZ%22%5D+%3D+%22N%5C%3B&input=int+foo%5B%5D+%3D+%7B4%2C+8%2C+15%2C+16%2C+23%2C+42%7D%3B&test_suite=1&test_suite_input=int+foo%5B%5D+%3D+%7B4%2C+8%2C+15%2C+16%2C+23%2C+42%7D%3B%0Ashort+array%5B%5D+%3D+%7B4%2C+3%2C+2%2C+1%7D%3B%0Aspam+EGGS%5B%5D+%3D+%7B42%7D%3B%0Aterrible_long_type_name+awful_array_name%5B%5D+%3D+%7B7%2C+-8%2C+1337%2C+0%2C+13%7D%3B&debug=0)
Regular expression and string chopping. Not particularly clever.
Explanation:
```
++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.
```
[Answer]
# Retina, ~~108~~ ~~104~~ ~~100~~ 69 bytes
Byte count assumes ISO 8859-1 encoding.
```
].+{((\S+ ?)+)
$#2];$1
+`((\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!](http://retina.tryitonline.net/#code=XS4reygoXFMrID8pKykKJCMyXTskMQorYCgoXHcrXFspLis7KFxTKyApKikoLT9cZCspLisKJDHCtiQyJCMzXSA9ICQ0Ow&input=dGVycmlibGVfbG9uZ190eXBlX25hbWUgYXdmdWxfYXJyYXlfbmFtZVtdID0gezcsIC04LCAxMzM3LCAwLCAxM307)
[Answer]
# C, 215 bytes, 196 bytes
*19 bytes saved thanks to @tucuxi!*
**Golfed:**
```
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);}
```
**Ungolfed:**
```
/*
 *  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;
main()
{
    /* 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 );
}
```
**Link:**
<http://ideone.com/h81XbI>
**Explanation:**
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 )
```
where:
* `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};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
00000000001111111111222222222233333
01234567890123456789012345678901234
```
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 ]
```
[Answer]
# V, 37 Bytes
```
2Eé0òYp6ldf ò$dT]ddÎdwf{xwC;
gg"1P
```
V is a 2D, string based golfing language that I wrote, designed off of vim. This works as of [commit 17](https://github.com/DJMcMayhem/V/commit/3e52ff77850f2d7897167eb9a20a859ca0169968).
Explanation:
This is pretty much a direct translation of [my vim answer](https://codegolf.stackexchange.com/a/77862/31716), 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
```
[Answer]
# C, ~~195~~ 180 bytes
195-byte original:
golfed:
```
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);}
```
ungolfed:
```
char*a,*b,*c,*d;
j;
main(i){
    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);}
```
ungolfed:
```
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);
}
```
Uses [bnf679's](https://codegolf.stackexchange.com/a/77983/41288) idea of appending to a string to avoid having to count commas.
[Answer]
## Python 3.6 (pre-release), 133
```
m,p=str.split,print;y,u=m(input(),'[');t,n=m(y);i=m(u[5:-2],', ')
l=len(i);p(t,n+f'[{l}];')
for x in range(l):p(n+f'[{x}] = {i[x]};')
```
Makes heavy use of [f-strings](https://www.python.org/dev/peps/pep-0498/).
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]};')
```
[Answer]
# 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};"}
```
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), ~~52~~ ~~50~~ 47 bytes
Code:
```
… = ¡`¦¨¨ð-',¡©gr¨s«„];«,®v¹ð¡¦¬s\¨N"] = "y';J,
```
Uses **CP-1252** encoding. [Try it online!](http://05ab1e.tryitonline.net/#code=4oCmID0gwqFgwqbCqMKow7AtJyzCocKpZ3LCqHPCq-KAnl07wqsswq52wrnDsMKhwqbCrHNcwqhOIl0gPSAieSc7Siw&input=aW50IGZvb1tdID0gezQsIDgsIDE1LCAxNiwgMjMsIDQyfTs).
[Answer]
## 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.)
[Answer]
# Pyth - ~~53~~ ~~50~~ ~~46~~ ~~45~~ 44 bytes
*2 bytes saved thanks to @FryAmTheEggman.*
```
+Jhcz\[+`]lKcPecz\{d\;j.es[ecJd`]kd\=dPb\;)K
```
[Test Suite](http://pyth.herokuapp.com/?code=%2BJhcz%5C%5B%2B%60%5DlKcPecz%5C%7Bd%5C%3Bj.es%5BecJd%60%5Dkd%5C%3DdPb%5C%3B%29K&input=int+foo%5B%5D+%3D+%7B4%2C+8%2C+15%2C+16%2C+23%2C+42%7D%3B&test_suite=1&test_suite_input=int+foo%5B%5D+%3D+%7B4%2C+8%2C+15%2C+16%2C+23%2C+42%7D%3B%0Ashort+array%5B%5D+%3D+%7B4%2C+3%2C+2%2C+1%7D%3B%0Aspam+EGGS%5B%5D+%3D+%7B42%7D%3B%0Aterrible_long_type_name+awful_array_name%5B%5D+%3D+%7B7%2C+-8%2C+1337%2C+0%2C+13%7D%3B&debug=0).
[Answer]
# [Pip](https://github.com/dloscutoff/pip), ~~48~~ 47 bytes
```
qR`(\S+)(. = ).(.+)}`{[b#Yd^k']';.n.b.,#y.c.y]}
```
Takes input from stdin and prints to stdout.
### Explanation
**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` = `", "`, `Y`ank 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 `R`eplacement, it is implicitly cast to a string. The default list-to-string conversion in Pip is concatenating all the elements:
```
"foo[6];
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.
[Answer]
## 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;
```
[Answer]
## 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 `-replace`s 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 `-join`ing 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.
### Example
```
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;
```
[Answer]
## C,~~278~~ 280 bytes
golfed:
```
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);}}}
```
ungolfed:
```
/* global ints
 * x = generic counter
 * e = number of elements
 * l = length of the array type
 * d = array defination upto the first '['
 */
x,e,l,d;
/* 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=='-';}
main(){
    gets(i);
    m=r=&i;
    while(*r++!=32);                // skip first space
    a=r;
    while(*++r!=93);                // skip to ']'
    l=r-a;
    d=r-m;
    for(;*r++;*r==44?e++:1);        // count elements
    printf("%.*s%d];\n",d,m,e+1);   // print array define
    r=&i;
    while(*r++) {                   // print elements
        if(c(*r)) {                 // is char a - or a digit?
            m=r;
            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...
[Answer]
# 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:
```
{
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
}
```
* 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, ….
[Answer]
# 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`
`)
```
[Answer]
## bash, ~~133~~ 129 bytes
```
read -a l
d="${l[@]:0:2}"
e=("${l[@]:3}")
echo "${d%?}${#e[@]}];"
for i in "${!e[@]}"
{
echo "${l[0]}[$i] = ${e[$i]//[!0-9]/};"
}
```
First attempt, sure its posible to get it shorter.
[Answer]
# 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, ";");
}
```
[Answer]
# 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.
Ungolfed:
```
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])
end
```
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!
[Answer]
## Python 2, 159 bytes
```
s=input().split()
t,n,v=s[0],s[1][:-2],''.join(s[3:])
a=v[1:-2].split(',')
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](http://ideone.com/fork/mm2tbz)
Thanks Kevin Lau for some golfing suggestions
[Answer]
## Python 3, 116 bytes
```
t,v,_,*l=input().split();v=v[:-1]+'%s]'
print(t,v%len(l)+';');i=0
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('] = {')
l=eval(b+',')
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.
[Answer]
## PHP, 143 bytes
**Golfed**
```
<?$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];";
```
**Ungolfed**
```
<?  
$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};"
```
Output:
```
int foo[6];foo[0] = 4;foo[1] = 8;foo[2] = 15;foo[3] = 16;foo[4] = 23;foo[5] = 42;
```
[Answer]
# [MATL](https://github.com/lmendo/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!**](http://matl.tryitonline.net/#code=J1x3KydYWDI6SCMpWEt4WElaYydbJ0tuViddOyd2IUsiSTJYKSdbJ1hAcVYnXSA9ICdAZyc7JzYkaA&input=J2ludCBmb29bXSA9IHs0LCA4LCAxNSwgMTYsIDIzLCA0Mn07Jw)
```
          % 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
```
[Answer]
## 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 :/
]