宣告複雜陣列 (array) 與雜湊 (hash) 及存取內容的方法

1. ARRAYS OF ARRAYS

@AoA = (
	[ "fred", "barney" ],
	[ "george", "jane", "elroy" ],
	[ "homer", "marge", "bart" ],
);

$AoA_ref = [
	[ "fred", "barney" ],
	[ "george", "jane", "elroy" ],
	[ "homer", "marge", "bart" ],
];

存取方法:
$AoA[1][0] = "george";
$AoA_ref->[1][0] = "george";

2. HASHES OF ARRAYS

%HoA = (
	carmans  => [ "fred", "barney" ],
	jetsons  => [ "george", "jane", "elroy" ],
	simpsons => [ "homer", "marge", "bart" ],
);

$HoA_ref = {
	carmans  => [ "fred", "barney" ],
	jetsons  => [ "george", "jane", "elroy" ],
	simpsons => [ "homer", "marge", "bart" ],
};

存取方法:
$HoA{jetsons}[0] = "george";
$HoA_ref->{jetsons}[0] = "george";

3. ARRAYS OF HASHES

@AoH = (
	{
		Lead => "fred",
		Pal  => "barney",
	},
	{
		Lead => "george",
		Wife => "jane",
		Son  => "elroy",
	},
	{
		Lead => "homer",
		Wife => "marge",
		Son  => "bart",
	}
);

$AoH_ref = [
	{
		Lead => "fred",
		Pal  => "barney",
	},
	{
		Lead => "george",
		Wife => "jane",
		Son  => "elroy",
	},
	{
		Lead => "homer",
		Wife => "marge",
		Son  => "bart",
	}
];

存取方法:
$AoH[1]{Lead} = "george";
$AoH_ref->[1]{Lead} = "george";

4. HASHES OF HASHES

%HoH = (
	carmans  => {
		Lead => "fred",
		Pal  => "barney",
	},
	jetsons  => {
		Lead => "george",
		Wife => "jane",
		Son  => "elroy",
	},
	simpsons => {
		Lead => "homer",
		Wife => "marge",
		Son  => "bart",
	},
);

$HoH_ref = {
	carmans  => {
		Lead => "fred",
		Pal  => "barney",
	},
	jetsons  => {
		Lead => "george",
		Wife => "jane",
		Son  => "elroy",
	},
	simpsons => {
		Lead => "homer",
		Wife => "marge",
		Son  => "bart",
	},
};

存取方法:
$HoH{jetsons}{Lead} = "george";
$HoH_ref->{jetsons}{Lead} = "george";

Posted by 搗蛋鬼 at 痞客邦 PIXNET Guestbook(0) 人氣()