My coworker, Jimmy is kinda new to C/C++. He's also kind of a slow learner. Now, to be fair, his code always compiles, but he has some really sloppy habits. For example, everybody knows that you can define an array like this:
int spam[] = {4, 8, 15, 16, 23, 42};
Everybody that is, except for Jimmy. He is convinced that the only way to make an array is like this:
int spam[6];
spam[0] = 4;
spam[1] = 8;
spam[2] = 15;
spam[3] = 16;
spam[4] = 23;
spam[5] = 42;
I keep fixing this for him in code-review, but he won't learn. So I need you to write a tool that automagically does this for him when he commits¹.
The challenge
I want you to write either a full program or a function that takes in a multiline string as input, and outputs the more compact version of the C array. The input will always follow this format, whitespace included:
identifier_one identifier_two[some_length];
identifier_two[0] = some_number;
identifier_two[1] = some_number;
identifier_two[2] = some_number;
...
identifier_two[some_length - 1] = some_number;
In short, the input will always be valid and well defined C. In more detail:
All of the identifiers will be made up of just letters and underscores. The length will always be at least one, and there will never be any missing or out of bounds indexes. You may also assume that the indexes are in order. For example:
foo bar[3];
bar[0] = 1
bar[2] = 9;
foo bar[1];
bar[0] = 1;
bar[1] = 3;
and
foo bar[3];
bar[2] = 9;
bar[0] = 1
bar[1] = 3
are all invalid inputs, and may cause undefined behavior in your submission. You may also assume that all of the numbers will be valid decimal numbers, negative or positive. The input will not have extraneous spaces. The output should always follow this format, whitespace included:
identifier_one identifier_two[] = {n1, n2, n3, ...};
Here is some sample data:
Input:
spam eggs[10];
eggs[0] = 0;
eggs[1] = 4;
eggs[2] = 8;
eggs[3] = -3;
eggs[4] = 3;
eggs[5] = 7;
eggs[6] = 888;
eggs[7] = 555;
eggs[8] = 0;
eggs[9] = -2;
Output:
spam eggs[] = {0, 4, 8, -3, 3, 7, 888, 555, 0, -2};
Input:
char ans[2];
ans[0] = 52;
ans[1] = 50;
Output:
char ans[] = {52, 50};
Input:
blah_blah quux[1];
quux[0] = 105;
Output:
blah_blah quux[] = {105};
You may take your input and output in any reasonable format, such as STDIN/STDOUT, function arguments and return value, reading and writing files etc. Standard loopholes apply. The shortest answer in bytes wins!
¹This is passive-aggressive and a terrible idea. You did not get this idea from me.