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)


In reply to Re: Simple menu.. by jptxs
in thread Simple menu.. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.