My first crack at this may be more comprehensible:

use Scalar::Util qw( looks_like_number ); use List::Util qw( first ); my @menu = ( 'Known star', 'Create a new star', 'List Known Stars', 'Q +uit' ); my $input = '...'; my $picked; if ( looks_like_number $input ) { $picked = $menu[ $input - 1 ]; } else { $picked = first { length $input <= length $_ and lc $input eq lc substr $_, 0, length $input; } @menu; } if ( !defined $picked ) { die "What do you mean by '$input'?"; }

Basically, if the input looks like a number, it's used as an index into the array of menu items. Otherwise, it loops through the menu looking for the first thing that "matches" the input.

My rewrite made this more useful as a menu in real code. Each menu item is now a hash ref that has a reference to a sub to handle the menu item. If need be, you can add more "stuff" to each hash ref. For example, maybe instead of substrings, you want to have regular expressions match the user's input. In that case, you could have a qr// stuck on each one used in the first loop instead of lc substr.

use Scalar::Util qw( looks_like_number ); use List::Util qw( first ); my @menu = ( { title => 'Known star', handler => \&load_star }, { title => 'Create a new star', handler => \&create_star }, { title => 'List Known Stars', handler => \&list_stars }, { title => 'Quit', handler => sub { exit } } ); my $input = '...'; my $picked; if ( looks_like_number $input ) { $picked = $menu[ $input - 1 ]; } else { $picked = first { length $input <= length $_->{title} and lc $input eq lc substr $_->{title}, 0, length $input; } @menu; } if ( !defined $picked ) { die "What do you mean by '$input'?"; } # "call" the menu item $picked->{handler}->();

In reply to Re: World Builder: the recovery and archeology of old programs. by kyle
in thread World Builder: the recovery and archeology of old programs. by dwm042

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.