📄 fig04_18.pl
字号:
#!/usr/bin/perl
# Fig. 4.18: fig04_18.pl
# Demonstrating functions delete, exists and defined.
%hash = ( Karl => 12,
Joe => 43,
Shawn => 0,
Paul => 11,
Bill => undef );
# obtain the list of hashKeys and display each key-value pair
@hashKeys = keys( %hash );
for ( $i = 0; $i < @hashKeys; ++$i ) {
print "$hashKeys[ $i ] => $hash{ $hashKeys[ $i ] }\n";
}
# delete the element with key 'Joe' from %hash
delete( $hash{ 'Joe' } );
# this loop determines if each key exists, is defined and
# whether the value for the key evaluates to true or false
while ( $key = pop( @hashKeys ) ) {
print "\n";
# determine if a particular key exists
if ( exists( $hash{ $key } ) ) {
print "$key exists in the hash.\n";
}
else {
print "$key doesn't exist in the hash.\n";
}
# determine if the value for the key is defined
if ( defined( $hash{ $key } ) ) {
print "$key is defined as $hash{ $key }.\n";
}
else {
print "$key is undefined.\n";
}
# determine if the value for the key is true or false
if ( $hash{ $key } ) {
print "$key is true.\n";
}
else {
print "$key is false.\n";
}
}
###########################################################################
# (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. #
# All Rights Reserved. #
# #
# DISCLAIMER: The authors and publisher of this book have used their #
# best efforts in preparing the book. These efforts include the #
# development, research, and testing of the theories and programs #
# to determine their effectiveness. The authors and publisher make #
# no warranty of any kind, expressed or implied, with regard to these #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or #
# consequential damages in connection with, or arising out of, the #
# furnishing, performance, or use of these programs. #
###########################################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -