Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am having trouble with this...its gotta be something simple but I just don't seem to be able to put my finger on it. I am very new to programming as well as perl.

here is my code:

use strict; my %commands = ( "command1" => \&sub1, "command2" => \&sub2, "command3" => \&sub3, "command4" => \&sub4, "command5" => \&sub5, "command6" => \&sub6, "exit" => sub { die "Goodbye" } ); while(1){ print "1. command1\n"; print "2. command2\n"; print "3. command3\n"; print "4. command4\n"; print "5. command5\n"; print "6. command6\n"; print "7. exit\n"; print "\n\n"; print "Please choose an option from the menu above: "; chomp(my $commands = <STDIN>); if ($commands{my $string}) { $commands{$string}->(); } else { print "No such command: $string\n"; } }

it keeps printing "No such command" no matter what I type...can't put my finger on it...I left out the subs from the above code because I figured it was irrelivent.

Any help would be great!

2001-04-21 Edit by Corion : Added CODE tags and formatting

Replies are listed 'Best First'.
Re: Simple menu..
by jptxs (Curate) on Oct 11, 2000 at 02:37 UTC

    with code tags :)

    use strict; my %commands = ( "command1" => \&sub1, "command2" => \&sub2, "command3" => \&sub3, "command4" => \&sub4, "command5" => \&sub5, "command6" => \&sub6, "exit" => sub { die "Goodbye" } ); while(1){ print "1. command1\n"; print "2. command2\n"; print "3. command3\n"; print "4. command4\n"; print "5. command5\n"; print "6. command6\n"; print "7. exit\n"; print "\n\n"; print "Please choose an option from the menu above: "; chomp(my $commands = ); if ($commands{my $string}) { $commands{$string}->(); } else { print "No such command: $string\n"; } }

    well, I'm not sure where the rest of this is, but if I'd have to guess i'd say the chomp(my $commands = ); and the if ($commands{my $string}) are your problem. The first is a mystery. I'm guessing you wanted something like:

    my $commands = <>; chomp $commands;
    so you could get user input and then use it to grab a sub from your hash. The second just looks like you got a bit mixed up (as i often do :). You cannot use my inside functions like that. It's its own entity and would need its own line. You could set its value with a function like:
    my $this = &that($thing);
    But not that way you have it. Also, you'd need to have the variable in the hash key defined before using it - like you use it in the command after the if line.

    hope that helps.

    -- I'm a solipsist, and so is everyone else. (think about it)

RE: Simple menu..
by chromatic (Archbishop) on Oct 11, 2000 at 02:59 UTC
    Do you want people to type simple digits, or strings like 'command1' and 'command2'?

    The way it's phrased in your menu, I'd expect to type '1' and have sub1() execute.

    Unfortunately, I'd have to type 'command1' to hit the correct hash key.

    If my assumption is incorrect, please respond with the data you've entered.

    Update: I completely missed this the first time. What you *really* want is:

    chomp(my $string = <STDIN> ); if ($commands{$string}) { $commands{$string}->(); } else { print "No such command: $string\n"; }
    You were assigning the input to $commands, but not doing anything with it. You also declared $string within the hash check, assuring that it was undefined.

    You did get the chomp part correct, which was the problem I had when I first tried something like this, though.

      Sorry for the sloppy post. I want to type in 'command1' and have it run that subroutine.
        You need to dereference the function name, see 11.4 in the cookbook # entering 'happy' runs the routine 'joy' eg %command = ( "happy" => \&joy ); chomp($feeling = <STDIN>); if($command{$feeling}){ $command{$feeling}->(); } sub joy{ print "happy huh \?"; }