Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
P is for Practical
 
PerlMonks

World Builder: the recovery and archeology of old programs.

by dwm042 (Curate)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Sep 17, 2008 at 15:37 UTC ( #712038=perlquestion: print w/ replies, xml ) Need Help??
dwm042 has asked for the wisdom of the Perl Monks concerning the following question:

I've been losing sleep at nights as I've been trying to rewrite the program World Builder into Perl. There are lots of issues with this, such as 'why bother'? My answers are that (1) because I'm emotionally caught up in the project and I won't sleep until I finish, and (2) that posted listings have serious coding errors as a consequence of mindless scanning of the article. But other than that, the conversion of a 25 year old Basic program into something that resembles sane Perl creates some opportunities to examine interface issues.

The main menu looks something like this:

WORLD BUILDER 1. Known star 2. Create a new star 3. List Known Stars 4. Quit
Fair enough. I'd like to be able to type '1' here, or '2' here, but I'd also like to be able to type 'KNOWN' or 'Known', or 'known', or any minimal increment of 'known' that is unique and end up in the same place. This has to be a well understood problem, so I'm asking for help here.

Thanks in advance,

David.

Comment on World Builder: the recovery and archeology of old programs.
Download Code
Re: World Builder: the recovery and archeology of old programs.
by Skeeve (Priest) on Sep 17, 2008 at 15:46 UTC

    You could solve that with substrings and lc / uc. Just compare the entered string with the a substring from the menu options, starting at 0 and having the same length your entered string has. Convert both strings to lower (or upper) case before comparing.

    Or you could compare each menu string with /^$entered_string/i. But take care to quotemeta the entered string so that entering "." or ".*" doesn't confuse your program.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
[reply]
[d/l]
[select]
Re: World Builder: the recovery and archeology of old programs.
by kyle (Abbot) on Sep 17, 2008 at 16:02 UTC

    My first crack at this may be more comprehensible:

    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}->();
[reply]
[d/l]
[select]

      kyle, I think that looping through the array you should not take the first match, but also verify that it is the only match. This to enforce the "minimal unique" requirement: if there are two options beginning with "kn" then "kn" is not a valid input but perhaps "kno" is.

      Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

[reply]

        If you want to be sure you have only one match, do it this way...

        my @picks = grep { ... } @menu; if ( 1 < scalar @picks ) { die "Too many options match your input, '$input'\n"; } $picked = $picks[0];
[reply]
[d/l]
      I wanted to thank you for your answer, kyle. I thought it interesting that you naturally gravitated to the use of a dispatch table in the menu, since the original Basic seems to be amenable to that approach. It doesn't hurt that Stephen Kimmel just about begins every functional block with a 'CLS' call.

      I'd show code, but until I get an answer from Ziff-Davis, (they own the Creative Computing IP) I'll have to just show bits and pieces, stuff I've written.

[reply]
        This is a bit of an afterward to this thread.

        1. I contacted Ziff-Davis who didn't feel they had the copyright.

        2. I contacted Steven Kimmel, who asked me to give him a copy of the program to look at before giving permission to post.

        3. He has recently given me permission to post.

        4. I have posted the translated code on my web site.

[reply]
Re: World Builder: the recovery and archeology of old programs.
by tod222 (Scribe) on Sep 17, 2008 at 18:44 UTC
    Ahh, Creative Computing, that brings back memories. It was a great magazine, I was sorry to see it fail.
[reply]
Re: World Builder: the recovery and archeology of old programs.
by planetscape (Abbot) on Sep 17, 2008 at 19:32 UTC

    I am wondering if, perhaps, this page might offer a better original listing: USML / Planetary Stuff ?

    HTH,

    planetscape
[reply]
      The answer is yes, it is a better original listing. They don't confuse numbers like '1' with the letter 'L'. L, in World Builder, is used for stellar luminosity.

      Update: Even this version has typos enough that it would not run accurately if not corrected.
[reply]
Re: World Builder: the recovery and archeology of old programs.
by GrandFather (Bishop) on Sep 17, 2008 at 21:07 UTC

    Why not provide a Tk (or other) GUI instead? It needen't be all that hard. Consider:

    use strict; use warnings; use Tk; use Tk::DialogBox; use Tk::Listbox; my $mw = MainWindow->new (); my %starData; my @params = ($mw, \%starData, ); $mw->Button (-text => 'Known star', -command => [\&knownStar, @params] +)->pack (); $mw->Button (-text => 'New star', -command => [\&newStar, @params])->p +ack (); $mw->Button (-text => 'List stars', -command => [\&listStars, @params] +)->pack (); $mw->Button (-text => 'Quit', -command => [\&quit, @params])->pack (); MainLoop (); sub knownStar { my ($mw, $starData) = @_; my @stars = map {[split ',']} split "\n", <<STARS; Sol, G2,1.0 Alpha Centuri A, G4,1.08 Alpha Centuri B, K1,.88 Epsilon Eridani, K2,.30 Tau Ceti, G8, .82 70 Ophiuchi A, K1,.9 70 Ophiuchi B, K5,.65 Eta Cassiopeiae A,f9,.94 Eta Cassiopeiae B,k6,.58 Sigma Draconis, G9,.82 36 Ophiuchi A, K2, .77 36 Ophiuchi B, K1,.76 Hr 7703, K2, .76 Delta Pavonis, G7,.98 82 Eridani, G5,.91 Beta Hydri, G1,1.23 Hr 8832, K3, .74 STARS my @names = map {$_->[0]} @stars; my $dlg = $mw->DialogBox ( -title => 'Select known star', -default_button => 'OK', -buttons => ['OK', 'Cancel'], ); my $listbox = $dlg->add ('Listbox', -listvariable => \@names, -selectmode => 'single' )->pack (); return if $dlg->Show () eq 'Cancel'; my @sel = $listbox->curselection (); return unless @sel; print "@{$stars[$sel[0]]}\n"; $starData->{$sel[0]} = $stars[$sel[0]]; } sub newStar { my ($mw, $starData) = @_; # Generate new star data here } sub listStars { my ($mw, $starData) = @_; } sub quit { my ($mw, $starData) = @_; # Save handling here if required exit; }

    Perl reduces RSI - it saves typing
[reply]
[d/l]

Back to Seekers of Perl Wisdom


Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: perlquestion [id://712038]
Approved by Corion
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others cooling their heels in the Monastery: (7)
GrandFather
wfsp
atcroft
herveus
Eyck
gnosti
im2
As of 2009-11-21 09:29 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls