This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Shazz's profile picture

Need Help Converting Arrays

Started by Shazz, 17 June 2013 - 08:02 PM
Shazz #1
Posted 17 June 2013 - 10:02 PM
Okay, so I'm working on this project and I need people to translate this array into their favourite programming languages.
You need to put it in a readable format and exactly how you would write it if you were to declare it (so it actually compiles/executes).
Please no esoteric programming languages.
Thank you very much!

Here is the array in JSON format (if you don't understand/know JSON then check the spoiler):

{
   "strVal":"Hello world!",
   "intVal":42,
   "floatVal":3.14,
   "boolVal":false,
   "nullVal":null,
   "0":"randomValue",
   "arrayCeption":[
	  "Hello world!",
	  42,
	  3.14,
	  false,
	  null
   ]
}

What I have so far:
SpoilerLua (by Shazz):

array = {
  strVal = 'Hello world!',
  intVal = 42,
  floatVal = 3.14,
  boolVal = false,
  nullVal = nil,
  'randomValue',
  arrayCeption = {
	'Hello world!',
	42,
	3.14,
	false,
	nil
  }
}
PHP (by Shazz):

$array = array(
  'strVal' => 'Hello world!',
  'intVal' => 42,
  'floatVal' => 3.14,
  'boolVal' => false,
  'nullVal' => NULL,
  'randomValue',
  'arrayCeption' => array(
   'Hello world!',
   42,
   3.14,
   false,
   NULL
  )
);
Javascript (same as JSON pretty much) (by Shazz):

var array = {
   "strVal":"Hello world!",
   "intVal":42,
   "floatVal":3.14,
   "boolVal":false,
   "nullVal":null,
   "0":"randomValue",
   "arrayCeption":[
   "Hello world!",
   42,
   3.14,
   false,
   null
   ]
}
Java (by Engineer):

import java.util.HashMap;

public class Main {
        public static void main(String[] args){
                HashMap<String, Object> table = new HashMap<String, Object>();
                table.put("strVal", "Hello world!");
                table.put("intVal", 42);
                table.put("floatVal", 3.14);
                table.put("boolVal", false);
                table.put("nullVal", null);
                table.put("0", "randomValue");
                table.put("arrayCeption", new Object[] {"Hello world!", 42, 3.14, false, null} );
        }
}
Python (by Kingdaro):

arr = dict(
  strVal = 'Hello world!',
  intVal = 42,
  floatVal = 3.14,
  boolVal = False,
  nullVal = None,
  '0' = 'randomValue',
  arrayCeption = ['Hello world!', 42, 3.14, false, None]
)
Ruby (by Kingdaro):

arr = {
  'strVal': 'Hello world!',
  'intVal': 42,
  'floatVal': 3.14,
  'boolVal': false,
  'nullVal': nil,
  '0': 'randomValue',
  'arrayCeption': ['Hello world!', 42, 3.14, false, nil]
}
Engineer #2
Posted 18 June 2013 - 02:48 AM
Java :)/>

import java.util.HashMap;

public class Main {
	public static void main(String[] args){
		HashMap<String, Object> table = new HashMap<String, Object>();
		table.put("strVal", "Hello world!");
		table.put("intVal", 42);
		table.put("floatVal", 3.14);
		table.put("boolVal", false);
		table.put("nullVal", null);
		table.put("0", "randomValue");
		table.put("arrayCeption", new Object[] {"Hello world!", 42, 3.14, false, null} );
	}
}
GravityScore #3
Posted 18 June 2013 - 08:08 AM
Objective C:

NSDictionary *arr = [NSDictionary dictionaryWithObjectsAndKeys:
  "Hello world!", "strVal",
  [NSNumber numberWithInt: 42], "intVal",
  [NSNumber numberWithFloat: 3.14], "floatVal",
  // Can't be bothered doing the rest, you get the point :P/>
  [NSArray arrayWithObjects: "hello world", [NSNumber numberWithInt: 42], [NSNumber numberWithFloat: 3.14], nil], "arrayCeption",
  nil];
Kingdaro #4
Posted 18 June 2013 - 04:41 PM
Python dictionary:

arr = dict(
  strVal = 'Hello world!',
  intVal = 42,
  floatVal = 3.14,
  boolVal = False,
  nullVal = None,
  '0' = 'randomValue',
  arrayCeption = ['Hello world!', 42, 3.14, False, None]
)

Ruby dictionary:

arr = {
  'strVal': 'Hello world!',
  'intVal': 42,
  'floatVal': 3.14,
  'boolVal': false,
  'nullVal': nil,
  '0': 'randomValue',
  'arrayCeption': ['Hello world!', 42, 3.14, false, nil]
}

The ruby dictionary is basically the python dictionary, except "false" is "False" and "nil" is "None". The "dict()" syntax is just cleaner in my opinion.
Shazz #5
Posted 18 June 2013 - 04:56 PM
Added them all, except Objective C - it's incomplete. I would try to complete it but that I don't know any Obj-C, it looks like complete gibberish to me. Thanks for contributing.