Here's a way without the external module. It's very basic, but could give you some guidance to make it more complex. Uses a dispatch table to run commands based on what the user enters, and will run the last command typed if user hits the up arrow.

Essentially, all you have to do is keep history in an array outside of your main game loop, then push to it every round. When the up arrow is seen, you need to pull the last entry from the history array, and use it.

#!/usr/bin/perl use strict; use warnings; my $up_arrow = "\e[A"; # up arrow my @history = []; my %dispatch = ( 'fight' => \&fight, 'run' => \&run, ); while(1){ print "Enter command...\n"; my $in = <STDIN>; chomp $in; if ($in eq $up_arrow){ print "$history[-1]"; push @history, $history[-1]; $dispatch{$history[-1]}->(); } else { push @history, $in; $dispatch{$in}->(); } } sub fight(){ print "Everybody was kung-fu fighting!\n"; } sub run(){ print "I'm running the hell away!\n"; }

Input/output:

steve02@steve02-ws:~$ ./arrow.pl Enter command... run I'm running the hell away! Enter command... ^[[A runI'm running the hell away! Enter command... fight Everybody was kung-fu fighting! Enter command... ^[[A fightEverybody was kung-fu fighting! Enter command...

EDIT: I often like to make code examples a little more complex than necessary to give requesters things to think about, but in this case, I may have gone a bit over the top. This is likely because now coding in Python, I like to exercise my Perl when I can :)

Here's a more basic example of how one could do what OP asked. Note that if *only* the most recent history is ever needed, it would be advisable to shift off of the history array each iteration as to not eat up all memory if the plan is to do many, many iterations.

The following code asks the user for a number, and prints the result of that number multiplied by 10. Hitting the up arrow will perform the same calculation with the same number as last round.

#!/usr/bin/perl use warnings; # always use this use strict; # always use this my $up_arrow = "\e[A"; # up arrow control character my @history = []; my $number = 10; while(1){ print "Enter a number...\n"; my $input = <STDIN>; chomp $input; if ($input eq $up_arrow){ $input = $history[-1]; } push @history, $input; my $result = $number * $input; print "\nResult of $input x $number is: $result\n"; }

Cheers,

-stevieb


In reply to Re: How to add input history by stevieb
in thread How to add input history by zsl

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.