Thread Modul für Auswahl (2 answers)
Opened by hlubenow at 2018-09-05 15:49

hlubenow
 2018-09-05 15:49
#188885 #188885
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Hallo,

öfters benutze ich eine Funktion zur Auswahl eines Elements aus einer Liste durch den Benutzer auf der Linux-Textkonsole.
In welcher Rubrik würde ich das als Modul im CPAN unterbringen? Vielleicht gibt's da schon sowas? Auf den ersten Blick konnte ich nichts finden.
Ist ja auch nichts Besonderes, aber wie gesagt, ich benutze es häufiger.
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl

use warnings;
use strict;

my @fruits = ("Apples", "Peaches", "Bananas");
my $choice = getChoice("Fruits", \@fruits, 2); 
print $fruits[$choice] . "\n\n";

sub getChoice {
    my $headline = shift;
    my $choicesref = shift;
    my $default = shift;
    system("clear");
    my @choices = @{$choicesref};
    my $x = 0;
    print "\n$headline:\n\n";
    for my $i (0 .. $#choices) {
        print $i + 1 . ". $choices[$i]\n";
    }   
    print "\n";
    my $inputstr = ""; 
    while ($x == 0) {
        print "Enter your choice";
        if ($default) {
            print " (Default: \"$default. $choices[$default - 1]\")";
        }
        print ": ";
        $inputstr = <STDIN>;
        chomp($inputstr);
        if ($inputstr eq "") {
            if ($default) {
                $inputstr = $default;
                $x = 1;
            }
            else {
                next;
            }
        }
        if ($inputstr eq "q") {
            print "Bye.\n\n";
            exit 0;
        }
        if ($inputstr !~ /\D/ &&
            $inputstr >= 1 &&
            $inputstr <= $#choices + 1) {
            $x = 1;
        }
    }
    print "\n";
    return $inputstr - 1;
}

View full thread Modul für Auswahl