Perl Learning - 10 (hash)
2012-11-13 14:41
337 查看
Hash is the one that makes Perl one of the most popular programer languages.
It's another data structure similar to array.
Hash is indexed by keys, each key is an uniq characters but not number array uses.
The keys have no order the numbers in array have.
Each key matches a value, keys must be uniq while values don't have to.
Keys are characters, but values can be any kind of scalars.
The way to access hash element: $hash{$some_key}
$family_name{"fred"}="flintstone";
$family_name{"barney"}="rubble";
foreach $person(qw<barney fred){
print "I've heard of $person $family_name{$person}.\n";
}
Hash has its own name space separated from scalar, array and subroutine.
Perl knows those things with same name, however we had better not use same name for different data types.
The key of hash can be any kind of expressons:
$foo="bar";
print $family_name{$foo."ney"}; # "rubble"
Giving a value to an existed key, the original one will be covered:
$family_name{"fred"}="astaire"; # new value
$bedrock=$family_name{"fred"}; # "astaire"
Accessing non-existed hash element will get undef.
We can give a list to a hash, or give a hash to a list:
%some_hash=("foo",35,"bar",12.4,2.5,"hello","wilma",1.72e30,"betty","bye\n");
@any_array=%some_hash;
The number of elements given to a hash must be even.
The order of @any_array usually is different from %some_hash. Hash doesn't care the order.
Like array, hash can be reversed, the key and value replaces each other.
%inverse_hash=reverse %any_hash;
To easily find out which are keys and which are values, we use "=>" in hashes.
my %last_name=(
"fred" => "flintstone",
"dino" => "undef",
"barney" => "rubble",
"betty" => "rubble",
);
my @array=%last_name;
print "@array\n";
The last ',' ins't a must but recommended, since it will be better when adding new elemens to the hash.
Perl will check all the ',' and the last one.
"=>" is almost same as ',' the different is that words on the left of a '=>' is automatically "" quoted.
So the keys on the left of "=>" don't have to be quoted, the keys in $hash{ } don't have to be quoted either.
It's another data structure similar to array.
Hash is indexed by keys, each key is an uniq characters but not number array uses.
The keys have no order the numbers in array have.
Each key matches a value, keys must be uniq while values don't have to.
Keys are characters, but values can be any kind of scalars.
The way to access hash element: $hash{$some_key}
$family_name{"fred"}="flintstone";
$family_name{"barney"}="rubble";
foreach $person(qw<barney fred){
print "I've heard of $person $family_name{$person}.\n";
}
Hash has its own name space separated from scalar, array and subroutine.
Perl knows those things with same name, however we had better not use same name for different data types.
The key of hash can be any kind of expressons:
$foo="bar";
print $family_name{$foo."ney"}; # "rubble"
Giving a value to an existed key, the original one will be covered:
$family_name{"fred"}="astaire"; # new value
$bedrock=$family_name{"fred"}; # "astaire"
Accessing non-existed hash element will get undef.
We can give a list to a hash, or give a hash to a list:
%some_hash=("foo",35,"bar",12.4,2.5,"hello","wilma",1.72e30,"betty","bye\n");
@any_array=%some_hash;
The number of elements given to a hash must be even.
The order of @any_array usually is different from %some_hash. Hash doesn't care the order.
Like array, hash can be reversed, the key and value replaces each other.
%inverse_hash=reverse %any_hash;
To easily find out which are keys and which are values, we use "=>" in hashes.
my %last_name=(
"fred" => "flintstone",
"dino" => "undef",
"barney" => "rubble",
"betty" => "rubble",
);
my @array=%last_name;
print "@array\n";
The last ',' ins't a must but recommended, since it will be better when adding new elemens to the hash.
Perl will check all the ',' and the last one.
"=>" is almost same as ',' the different is that words on the left of a '=>' is automatically "" quoted.
So the keys on the left of "=>" don't have to be quoted, the keys in $hash{ } don't have to be quoted either.
相关文章推荐
- Perl Learning - 11 (hash keys, values, each, exists, delete)
- Learning Perl(Perl语言入门)学习笔记(10)
- Perl Learning 5 Hash
- Perl - Summary on built-in types : hash and has...
- Learning Perl: 2.3. Perl's Built-in Warnings
- Learning Perl: 5.4. Output to Standard Output
- Perl Learning - 14 (Catch var, (:?), (:<LABLE>), \g{LABLE}, {m,n}
- 用Perl的hash数组实现个性化监控
- Perl 中输出 hash 和 array
- perl基础:传递hash类型参数
- Learning Python(10)--input与raw_input
- 一个有关perl hash结构的how to
- Perl学习笔记 Learning Perl Chapter I
- Perl语言入门笔记 第六章 哈希(hash)
- perl hash 和 map 新理解
- Learning Perl 3
- Deep Learning of Binary Hash Codes for Fast Image Retrieval
- 转载 10 Python Machine Learning Projects on GitHub
- The 10 Algorithms Machine Learning Engineers Need to Know
- Perl Learning: 2.6. The if Control Structure