fig20_12.pl

来自「PERL语言资料 可以用于PERL程序设计」· PL 代码 · 共 85 行

PL
85
字号
#!/usr/bin/perl
# Fig. 20.12: fig20_12.pl
# Simple hash table implementation.

use warnings;
use strict;
use Hashtable;

my $ht = new Hashtable( 11 );
my $choice = '';

instructions();
print( "? " );
chomp( $choice = <STDIN> );

while ( $choice ne 'q' ) {
   
   if ( $choice eq 'i' ) {
      print( "Enter several strings on separate lines\n",
             "('DONE' to terminate input):\n" );
      chomp( my $data = <STDIN> );

      while ( $data ne 'DONE' ) {
         print( "Inserted '$data' into slot ", 
                $ht->insert($data), "\n" );
         chomp( $data = <STDIN> );
      }

      print( "\n" );
   }
   elsif ( $choice eq 'r' ) {
      print( "What element would you like to remove? " );
      chomp( my $data = <STDIN> );

      unless ( $ht->remove( $data ) ) {
         print( "Could not delete $data\n\n" );
      }
      else {
         print( "\n" );
      }
   }
   elsif ( $choice eq 'd' ) {
      $ht->printTable();
      print( "\n" );
   }
   elsif ( $choice eq '?' ) {
      instructions();
   }
   else {
      print( "Please enter a valid command. ",
             "Enter '?' for instructions.\n\n" );
   }

   print( "? " );
   chomp( $choice = <STDIN> );
}

sub instructions 
{
print <<DONE

Enter 'i' to insert numbers.
Enter 'r' to remove numbers.
Enter 'd' to display hash table.
Enter '?' to print these instructions.
Enter 'q' to quit.
DONE
}

###########################################################################
#  (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 + =
减小字号Ctrl + -
显示快捷键?