Pure Programmer
Blue Matrix


Cluster Map

Maps (aka Associative Arrays)

L1

This page is under construction. Please come back later.

Maps1.swift
#!/usr/bin/env swift;
import Foundation
import Utils

func main() -> Void {
	var dictionary:[String: String] = [:]

	print("dictionary size: " + String(dictionary.count))
	print("dictionary is " + (dictionary.isEmpty ? "empty" : "not empty"))
	dictionary["apple"] = "red"
	dictionary["banana"] = "yellow"
	dictionary["cantaloupe"] = "orange"
	dictionary["dragonfruit"] = "red"
	dictionary["elderberry"] = "purple"

	print("dictionary size: " + String(dictionary.count))
	print("dictionary is " + (dictionary.isEmpty ? "empty" : "not empty"))

	print("bananas are " + dictionary["banana"])

	var x:String = "dragonfruit"
	if dictionary.keys.contains(x) {
		print(x + " are " + dictionary[x])
	} else {
		print("Can't find " + x)
	}

	x = "fig"
	if dictionary.keys.contains(x) {
		print(x + " are " + dictionary[x])
	} else {
		print("Can't find " + x)
	}

	x = "elderberry"
	dictionary.removeValue(forKey: x)
	if dictionary.keys.contains(x) {
		print(x + " are " + dictionary[x])
	} else {
		print("Can't find " + x)
	}

	print(Utils.mapToString(dictionary))
	exit(EXIT_SUCCESS)
}
main()

Output
$ swiftc Maps1.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
Maps2.swift
#!/usr/bin/env swift;
import Foundation
import Utils

func main() -> Void {
	var planetDiameters:[String: Int] = [
		"Mercury": 4879,
		"Venus": 12103,
		"Earth": 12756,
		"Mars": 6794,
		"Jupiter": 142985,
		"Saturn": 120534,
		"Uranus": 51115,
		"Neptune": 49534,
		"Pluto": 2374,
		"Ceres": 946,
		"Eris": 2326,
		"Makemake": 1430
	]

	for planet in planetDiameters.keys {
		print(planet + " has a diameter of " + String(planetDiameters[planet]) + " km")
	}
	exit(EXIT_SUCCESS)
}
main()

Output
$ swiftc Maps2.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
swift

Questions

Projects

More ★'s indicate higher difficulty level.

References