New dictionary

Jmnote (토론 | 기여)님의 2018년 1월 15일 (월) 13:47 판 (→‎PHP)
Associative arrays
Hash
hash map
hash table
Dictionary
dict
NSDictionary
NSMutableDictionary

1 Bash

declare -A MEMBER
MEMBER=(["ID"]=102 ["Name"]="YONEZAWA Akinori" ["Address"]="Naha, Okinawa")

for k in "${!MEMBER[@]}"; do
  echo "[$k] ${MEMBER[$k]}"
done
# [Address] Naha, Okinawa
# [ID] 102
# [Name] YONEZAWA Akinori
declare -A MEMBER
MEMBER["ID"]=102
MEMBER["Name"]="YONEZAWA Akinori"
MEMBER["Address"]="Naha, Okinawa"
declare -A MEMBER
MEMBER=(["ID"]=102)
MEMBER+=(["Name"]="YONEZAWA Akinori")
MEMBER+=(["Address"]="Naha, Okinawa")

2 C#

Hashtable member = new Hashtable();
member.Add("ID", "102");
member.Add("Name", "YONEZAWA Akinori");
member.Add("Address", "Naha, Okinawa");
Dictionary<string, string> member = new Dictionary<string, string>();
member.Add("ID", "102");
member.Add("Name", "YONEZAWA Akinori");
member.Add("Address", "Naha, Okinawa");
Dictionary<int, string> member = new Dictionary<int, string> {
  { "ID", "102" },
  { "Name", "YONEZAWA Akinori" },
  { "Address", "Naha, Okinawa" }
};

3 Java

HashMap<String,Object> member = new HashMap<String,Object>();
member.put("ID", "102");
member.put("Name", "YONEZAWA Akinori");
member.put("Address", "Naha, Okinawa");
System.out.println( member );
// {Address=Naha, Okinawa, ID=102, Name=YONEZAWA Akinori}

4 JavaScript

literal notation
var member = {
'ID' : '102',
'Name' : 'YONEZAWA Akinori',
'Address' : 'Naha, Okinawa'
};
console.log(member);
// { ID: '102', Name: 'YONEZAWA Akinori', Address: 'Naha, Okinawa' }
var member = {
ID : '102',
Name : 'YONEZAWA Akinori',
Address : 'Naha, Okinawa'
};
console.log(member);
dot notation
var member = {}
member.ID = '102';
member.Name = 'YONEZAWA Akinori';
member.Address = 'Naha, Okinawa';
console.log(member);
var member = new Object();
member.ID = "102";
member.Name = "YONEZAWA Akinori";
member.Address = "Naha, Okinawa";
console.log(member);
bracket notation
var member = {}
member['ID'] = "102";
member['Name'] = "YONEZAWA Akinori";
member['Address'] = "Naha, Okinawa";
console.log(member);
var member = new Object();
member['ID'] = "102";
member['Name'] = "YONEZAWA Akinori";
member['Address'] = "Naha, Okinawa";
console.log(member);

5 Objective-C

NSMutableDictionary *member = [[NSMutableDictionary alloc] init];
[member setObject:@"102" forKey:@"ID"];
[member setObject:@"YONEZAWA Akinori" forKey:@"Name"];
[member setObject:@"Naha, Okinawa" forKey:@"Address"];

6 PHP

$member = array(
  "ID" => 102,
  "Name" => "YONEZAWA Akinori",
  "Address"=> "Naha, Okinawa"
);
$member = array();
$member['ID'] = 102;
$member['Name'] = "YONEZAWA Akinori";
$member['Address'] = "Naha, Okinawa";

7 Python

d = dict(a=1, b=2, c=3)
print( d )
# {'b': 2, 'c': 3, 'a': 1}
member = { 'ID' : 102, 'Name' : 'YONEZAWA Akinori', 'Address' : 'Naha, Okinawa' }
from pprint import pprint
member = { }
member['ID'] = 102
member['Name'] = 'YONEZAWA Akinori'
member['Address'] = 'Naha, Okinawa'
pprint( member )
# {'Address': 'Naha, Okinawa', 'ID': 102, 'Name': 'YONEZAWA Akinori'}

8 Ruby

string key
member = {
  "ID" => 102,
  "Name" => "YONEZAWA Akinori",
  "Address"=> "Naha, Okinawa"
}
puts member 
# {"ID"=>102, "Name"=>"YONEZAWA Akinori", "Address"=>"Naha, Okinawa"}
member = {}
member['ID'] = 102
member['Name'] = 'YONEZAWA Akinori'
member['Address'] = 'Naha, Okinawa'
puts member
# {'Address': 'Naha, Okinawa', 'ID': 102, 'Name': 'YONEZAWA Akinori'}
member = Hash.new
member['ID'] = 102
member['Name'] = 'YONEZAWA Akinori'
member['Address'] = 'Naha, Okinawa'
puts member
# {'Address': 'Naha, Okinawa', 'ID': 102, 'Name': 'YONEZAWA Akinori'}
symbol key
member = {
  ID: 102,
  Name: "YONEZAWA Akinori",
  Address: "Naha, Okinawa"
}
print member 
# {:ID=>102, :Name=>"YONEZAWA Akinori", :Address=>"Naha, Okinawa"}

9 Perl

my %month = (
	"January" => "1월",
	"February" => "2월",
	"March" => "3월",
	"April" => "4월",
	"May" => "5월",
	"June" => "6월",
	"July" => "7월",
	"August" => "8월",
	"September" => "9월",
	"October" => "10월",
	"November" => "11월",
	"December" => "12월"
);

$month{"January"} = "일월";
$month{"March"} = "삼월";

foreach my $key (keys %month) {
	my $value = $month{$key};
	print "$key : $value\n";
}
#February : 2월
#December : 12월
#June : 6월
#July : 7월
#November : 11월
#April : 4월
#May : 5월
#January : 일월
#August : 8월
#October : 10월
#March : 삼월
#September : 9월

10 같이 보기