Anthony Vanelverdinghe wrote:
>
> Hi
Hello,
> Could anyone please tell me what's wrong with the following "program"?
> The compiler gives errors in the switch statement.
>
> %commands=('v',0,'w',1,'t',2,'/pattern/',3,'s',4,'x',5);
> $end = 0;
> while (!end){
> print "bookmarks.html>";
> $operation = <>;
> chop $operation;
You should use chomp() instead of chop() unless you really need chop().
> $op=$commands{$operation};
> switch ($op) {
> case 0 {
> &add ();
> last;}
> case 1 {
> &delete();
> last;}
> case 2 {
> &show();
> last;}
> case 3 {
> &pattern();
> last;}
> case 4 {
> &save();
> last;}
> case 5 {
> &exit ();
> last;}
> }
> }
>
> sub add{
> print "add";
> }
> sub delete{
> print "delete";
> }
> sub show{
> print "show";
> }
> sub pattern{
> print "pattern";
> }
> sub save{
> print "save";
> }
> sub exit{
> $end = 1;
> }
Note that your exit sub may not work as Perl already has an exit
function.
You could use a dispatch table for that:
sub add { print 'add' }
sub delete { print 'delete' }
sub show { print 'show' }
sub pattern { print 'pattern' }
sub save { print 'save' }
my %commands = (
v => \&add,
w => \&delete,
t => \&show,
'/pattern/' => \&pattern,
s => \&save,
);
while ( my $operation = <> ) {
print 'bookmarks.html>';
chomp $operation;
last if $operation eq 'x';
$commands{ $operation }->() if exists $commands{ $operation };
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>