Wednesday, February 20, 2008

Learning Perl, 4th Edition Notes 0x5

Learning Perl, 4th Edition Notes 0x5 by DaNmarner * hash is the data structure that stores the key/value data pairs without order. * Syntax to access a hash element:
$hash_name{$key}
* Accessing an unassigned hash element will yield undef. * Hash name is leaded by a percent sign (%) when treated as a variable, like the at sign (@) for arrays. * In a list context, hash appears as a list that consist of key/value pairs in a random sequence. * To switch kay and value in hashs, use reverse:

%a_hash = reverse %a_hash;

* For convenience, the big arrow (=>) can be used to show the relation between kay and value in literal hashs.

$a_hash = ( "localhost" => '172.0.0.1', );
* the key function returns a key list of a hash, while the the values function returns the values. * The each function can be used to iterate a hash, it returns a list consists of two elements each time it is used: a pair of kay/value. * The existence of a key can be tested by the function exists. * delete function will make a key disappear, but not assign undef to it.

2 comments:

Randal L. Schwartz said...

Your two instances of $a_hash should probably be %b_hash or something... they don't make sense as scalars.

DaNmarner said...

Oops, it's just miss-typing.....thanks, again!