| Bytes | Lang | Time | Link |
|---|---|---|---|
| 061 | MATL | 160415T193507Z | Luis Men |
| 069 | Uiua | 240908T201054Z | nyxbird |
| 111 | JavaScript | 240910T102147Z | Shaggy |
| 039 | Japt R | 240909T142727Z | Shaggy |
| 125 | JavaScript ES6 | 160415T192712Z | Conor O& |
| 037 | V | 160419T062556Z | DJMcMayh |
| 115 | Clojure | 170101T215710Z | NikoNyrh |
| 180 | C | 160418T014104Z | tucuxi |
| 196 | C | 160417T073956Z | homersim |
| 047 | Vim | 160415T191558Z | DJMcMayh |
| 143 | PHP | 160418T132144Z | Xanderha |
| 105 | PowerShell v2+ | 160415T194836Z | AdmBorkB |
| nan | Perl 5.10 | 160417T161036Z | nwellnho |
| 101 | Julia | 160415T190221Z | Alex A. |
| 188 | D | 160416T184216Z | Ben Perl |
| 047 | Pip | 160416T034314Z | DLosc |
| 280 | C | 160417T190721Z | bnf679 |
| 088 | Ruby | 160415T185559Z | Value In |
| 069 | Retina | 160415T193303Z | daavko |
| 044 | Pyth | 160415T191542Z | Maltysen |
| 044 | Pyth | 160415T202657Z | isaacg |
| 129 | bash | 160416T185259Z | bnf679 |
| 116 | Python 3 | 160416T072428Z | xnor |
| 100 | JavaScript ES6 | 160415T212945Z | Neil |
| 101 | Awk | 160415T204012Z | muru |
| 133 | Python 3.6 prerelease | 160415T200128Z | vaultah |
| 159 | Python 2 | 160415T184942Z | user4594 |
| 047 | 05AB1E | 160415T192216Z | Adnan |
MATL, 61 bytes
Thank you to @Shaggy for pointing out a mistake, now corrected.
'[-\w]+'XX2:H#)XKxXIZc'['KnV'];'v!K"I2X)'['X@qV'] = '@g';'6$h
This isn't C, but it does use C-like Nah, that was wasting 4 bytes.sprintf function
% 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
% Implicitly end for each
% Implicitly display
JavaScript, 111 bytes
A recursive approach that didn't work out quite as well as I hoped but nowhere near as bad as I feared.
f=(s,[x,y,...a]=s.match(/-?\w+\[?/g),n)=>y?(n?`;
${x+~-n}] = `+y:x+` ${y+a.length}]`)+f(s,[n?x:y,...a],-~n):`;`
Japt -R, 41 40 39 bytes
This started out horrible, and got progressively worse over a few hours! And, just as I had it down to a byte count I was somewhat happy with at least, I spotted the inclusion of the ; on each line of output and it got even more horrible!
Took a break for a bit, tried a fresh approach, and ended up with a one-liner that has * a better score than my original attempt. It's still a horrible solution, though!
f"-?%d+" £¸Åθp¥ÅXÃpU¸v2)£ÌiX¸r']ÈiT°
é
* That didn't last long!
f"..." £¸Åθp¥ÅXÃpU¸v2)£ÌiX¸r']ÈiT°\né :Implicit input of string U
f :Match
"..." : RegEx /-?[0-9]+/g
£ :Map each X
¸ : Split U on spaces
Å : Slice off the first element
Î : Get the first element
¸ : Split on spaces
p : Push
¥ : "=="
Å : Slice off the first character
X : And X
à :End map
p :Push
U¸ : Split U on spaces
v2 : Pop the first 2 elements
) :End push
£ :Map each X
Ì : Last character of U
i : Prepend
X¸ : X joined with spaces
r'] : Replace "]" by
È : Passing through a function
i : Prepend
T° : Postfix incremented T (initially 0)
\n :End map
é :Rotate right
:Implicit output joined with newlines
JavaScript ES6, 134 132 130 129 125 bytes
Saved 1 byte thanks to Neil. Fixed, thanks to l4m2 for noticing!
x=>(s=(m=x.match(/(\w+) (\w+).+{(.+)}/))[3].split`,`.map(e=>`${t=m[2]}[${i++}] = ${+e};`,i=0).join`
`,m[1]+` ${t}[${i}];
`+s)
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.
Explanation:
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
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 :/
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 idea of appending to a string to avoid having to count commas.
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:
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:
iis the input string (achar*)tis the pointer location offset ofixis the actualintparsed from the stringnis 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 ]
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];"
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;
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.
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;
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;
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!
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, ";");
}
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 = ", ", 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[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.
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...
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};"}
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.
Pyth - 53 50 46 45 44 bytes
2 bytes saved thanks to @FryAmTheEggman.
+Jhcz\[+`]lKcPecz\{d\;j.es[ecJd`]kd\=dPb\;)K
Pyth, 44 bytes
++Khcz\]lJ:z"-?\d+"1"];"VJs[ecKd~hZ"] = "N\;
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.
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.
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.
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.)
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 isNF - 3. - Then it's just printing a special line for the declaration, and looping over the numbers.
- I should assign
FSeither when invoking awk (using-F) or in aBEGINblock. In the interests of brevity, ….
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.
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]};')
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])
Thanks Kevin Lau for some golfing suggestions
05AB1E, 52 50 47 bytes
Code:
… = ¡`¦¨¨ð-',¡©gr¨s«„];«,®v¹ð¡¦¬s\¨N"] = "y';J,
Uses CP-1252 encoding. Try it online!.