in reply to Re: How to add input history
in thread How to add input history

Thanks, stevieb! I've updated my program to account for warnings raised when input lacks a modifier, but there's a problem. Am I using your technique right?

#!/usr/bin/perl use warnings; use strict; my $up = "\e[A"; my @history = []; print "Input the dice and modifier you want to roll like this: 2d9+100 +. To quit, press Ctrl-C.\n"; while (1) { my $dice = <>; chomp $dice; my @arr = split (/d/, $dice); if ($dice =~ /\+/) { my @arr2 = split (/\+/, $arr[1]); $arr2[0]++; for my $i (1 .. $arr[0]) { print ((int(rand($arr2[0] - 1)) + 1 + $arr2[1]), "\n"); } } else { $arr[1]++; for my $i (1 .. $arr[0]) { print ((int(rand($arr[1] - 1)) + 1), "\n"); } } push @history, $dice; }

Whenever I use this code, I get this problem:

zsl@pc:~/codefolder $ perl dieroller.pl Input the dice and modifier you want to roll like this: 2d9+100. To qu +it, press Ctrl-C. 2d9+100 101 101 ^[[A Argument "^[[A" isn't numeric in foreach loop entry at DDDR.pl line 19 +, <> line 2. ^C zsl@pc:~/codefolder $
$thanks++, zsl

Replies are listed 'Best First'.
Re^3: How to add input history
by zsl (Novice) on Apr 11, 2015 at 00:07 UTC

    Wait, oops. I forgot to add an extra if clause. The new code looks like:

    #!/usr/bin/perl use warnings; use strict; my $up = "\e[A"; my @history = []; my $number = 10; print "Input the dice and modifier you want to roll like this: 2d9+100 +. To quit, press Ctrl-C.\n"; while (1) { my $dice = <>; chomp $dice; my @arr = split (/d/, $dice); if ($dice eq $up) { $dice = $history[-1]; } elsif ($dice =~ /\+/) { my @arr2 = split (/\+/, $arr[1]); $arr2[0]++; for my $i (1 .. $arr[0]) { print ((int(rand($arr2[0] - 1)) + 1 + $arr2[1]), "\n"); } } else { $arr[1]++; for my $i (1 .. $arr[0]) { print ((int(rand($arr[1] - 1)) + 1), "\n"); } } push @history, $dice; }

    When I run it, the up arrow doesn't cause any error messages, but it doesn't do anything either:

    Input the dice and modifier you want to roll like this: 2d9+100. To qu +it, press Ctrl-C. 2d9+100 109 101 ^[[A ^C
    $thanks++, zsl