Schrift
[thread]8605[/thread]

Nicht numerische Variablen in Perl: Problem mit nicht numerischen Variablen



<< >> 6 Einträge, 1 Seite
Gast Gast
 2006-12-25 15:50
#72755 #72755
Hallo zusammen

Ich bin zum ersten mal in diesem Forum. Ich konnte mein Problem jedoch nicht als bereits gestellte Frage finden, deshalb poste ich hier nun mein Problem, in der Hoffnung mir kann geholfen werden =) ..

Ich hab folgendes Programm geschrieben (mit Perl natürlich, ps: ich bin Anfänger, deshalb ist es mit sehr einfachen Mitteln aufgebaut) Das Programm soll ein Feld aus Punkten mit z zeilen und s spalten ausgeben, wobei an der Stelle (1/1) ein * sein soll (dies hat soweit geklappt). Später sollte dann der User wie durch ein Spiel mittels Eingabe den Stern steuern können (daran arbeite ich noch), doch mein Problem ist nun folgendes: ich kann die Eingabe des Users nur als numerische Variable einfangen, jedoch nicht als "q" oder "o" (dies wäre mein Ziel, dass der User nicht 5, sondern q eingeben kann, damit das Programm beendet wird). Schreibe ich jedoch (if ($befehl == q) oder if($befehl == "q") gibt er eine Fehlermeldung zurück (no numeric ... oder so was ähnliches):


Code: (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
53
54
55
56
57
#!perl -w
use strict;
use warnings;

#Zeilen und Spaltenzahl wird durch den User definiert:

my $z;
print "Breite?";
$z = <STDIN>;

my $s;
print "Hoehe?";
$s = <STDIN>;

my $test;
$test = 1;

do
{
$b = 1;

while ($b <= $s)
{
$a = 1;
while ($a <= $z)
{
if ($a == 1 and $b == 1)
{

print "* ";
}
else
{

print ". ";
}

$a++
}
print "\n";
$b++
}

my $befehl;
print "Befehl? (1=Nord,2=Ost,3=Sued,4=West,5=Quit)";
$befehl = <STDIN>;
if ($befehl == 5)
{
print "Programm wurde beendet";
$test++;
}
elsif ($befehl == 1 or 2 or 3 or 4)
{
print "ok";
}
}
while ($test == 1)


Ps: ich arbeite auf eine WinXP rechner (falls dies eine Rolle spielt)

Herzlichen Dank für die Hilfe!

Gruss

von Sigibe =)
Relais
 2006-12-25 16:01
#72756 #72756
User since
2003-08-06
2244 Artikel
ModeratorIn
[Homepage] [default_avatar]
Lies mal perlintro - der Operator, den Du suchst, heißt eq.
Erst denken, dann posten --
26. Deutscher Perl- u. Raku -Workshop 15. bis 17.04.2024 in Frankfurt/M.

Winter is Coming
renee
 2006-12-25 19:12
#72757 #72757
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Das funktioniert so auch nicht.
Code: (dl )
1
2
3
4
    elsif ($befehl == 1 or 2 or 3 or 4)
{
print "ok";
}


Entweder
Code: (dl )
1
2
3
4
    elsif ($befehl =~ /^[1-4]$/)
{
print "ok";
}


oder
Code: (dl )
1
2
3
4
    elsif ($befehl == 1 or $befehl == 2 or $befehl == 3 or $befehl == 4)
{
print "ok";
}


oder
Code: (dl )
1
2
3
4
    elsif ($befehl >= 1 and $befehl <= 4)
{
print "ok";
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
PerlProfi
 2006-12-26 00:14
#72758 #72758
User since
2006-11-29
340 Artikel
BenutzerIn
[default_avatar]
Ich würde es anders angehen.

Vielleicht hilft dir das hier:
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/perl
use strict;
use warnings 'all';

# create the Map
my $map = new Map;

# set Map size and start position by user
$map->matrix()->position();

# MainLoop
MAIN:
while (1)
{
    # clear screen
    system($^O =~ /win/i ? "cls" : "clear");

    # draw the Map
    $map->draw();

    # get user input
    print "
1/w:forward\t2/s:back\t3/a:left\t4/d:right
5/q:quit
IN: ";
    chomp(my$in = <STDIN>);
    $in =~ s/\s*//;

    # evaluate user input
    if ($in =~ /^[1w]$/i)
    {
        # move forwards
        $map->move(0, -1);
    }
    elsif ($in =~ /^[2s]$/i)
    {
        # move backwards
        $map->move(0, 1);
    }
    elsif ($in =~ /^[3a]$/i)
    {
        # move leftwards
        $map->move(-1, 0);
    }
    elsif ($in =~ /^[4d]$/i)
    {
        # move rightwards
        $map->move(1, 0);
    }
    elsif ($in =~ /^[5q]$/i)
    {
        # stop MainLoop
        last MAIN;
    }
    else
    {
        print "
Wrong input: $in
";
        <STDIN>;
    }
} # MainLoop

# exit if the MainLoop is stopped
exit(0);

package Map;
use strict;
use warnings 'all';

#
# create a new object
#
sub new
{
    my $class = shift;

    my $obj =
    {
        matrix    => ["*"], # 1d matrix to save the Map data
        pos    => [1,1], # array with x and y position in the Map(count from 1)
        width    =>     1, # width of the matrix(count from 1)
        height    =>     1, # height of the matrix(count from 1)
    };
    bless $obj, $class;

    return $obj;
} # new

#
# create the matrix by given width and height
#
sub dat
{
    my $self = shift;
    my($width, $height) = @_;

    $self->{width}  = $width;
    $self->{height} = $height;
    $self->{matrix} = [split("","."x($width*$height))];

    return $self;
} # dat

#
# set the startposition by given x and y coordinate
#
sub pos
{
    my $self   = shift;
    my($x, $y) = @_;

    # we have a 1d matrix, so the index must be: x+width*y
    $self->{matrix}->[$self->{pos}->[0]-1+$self->{width}*($self->{pos}->[1]-1)] = ".";
    $self->{pos}   = [$x, $y];
    $self->{matrix}->[$x               -1+$self->{width}*($y               -1)] = "*";

    return $self;
} # pos

#
# set the matrix by user
#
sub matrix
{
    my $self = shift;

    print "Rows: ";
    chomp(my$r = <STDIN>);

    print "Columns: ";
    chomp(my$c = <STDIN>);

    return $self->dat($c, $r);
} # matrix

#
# set the startposition by user
#
sub position
{
    my $self = shift;

    print "Startrow: ";
    chomp(my$r = <STDIN>);

    print "Startcolumn: ";
    chomp(my$c = <STDIN>);

    return $self->pos($c, $r);
} # position

#
# change position with amount
#
sub move
{
    my $self     = shift;
    my($ax, $ay) = @_;

    # compute new position
    my $nx = $self->{pos}->[0]+$ax;
    my $ny = $self->{pos}->[1]+$ay;

    # if we are out of the Map, go top or respectively bottom
    $nx = $self->{width}  if $nx < 1;
    $ny = $self->{height} if $ny < 1;
    $nx = 1 if $nx > $self->{width} ;
    $ny = 1 if $ny > $self->{height};

    # set the new pos
    return $self->pos($nx, $ny);
} # move

#
# draw the matrix on screen
#
sub draw
{
    my $self = shift;

    for my$y (1..$self->{height})
    {
        for my$x (1..$self->{width})
        {
            print $self->{matrix}->[$x-1+$self->{width}*($y-1)] ." ";
        }
        print "
";
    }

    return $self;
} # draw

__END__

Es sollte so in etwa das sein, was du da versucht hast.

MfG PerlProfi

edit: Kann mir jemand sagen, wieso erst alle \n in meinem code zu ' ' umgewandelt wurden, und nach dem verändern vom Beitrag dann als Zeilenumbrüche dargestellt werden????\n\n

<!--EDIT|PerlProfi|1167085246-->
renee
 2006-12-26 11:39
#72759 #72759
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Bei so etwas kann man gut einen Hash als dispatcher einsetzen. Einfach alle erlaubten Eingaben in einen Hash schreiben und die Parameter fuer die move-Funktion als Wert. Dann ueberprueft man einfach, ob es fuer die Eingabe einen Eintrag im Hash gibt. Falls ja, fuehrt man das aus, ansonsten macht man einen default-Schritt.
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
PerlProfi
 2006-12-26 16:02
#72760 #72760
User since
2006-11-29
340 Artikel
BenutzerIn
[default_avatar]
Gute Idee, dann wird es um einiges kürzer.
<< >> 6 Einträge, 1 Seite



View all threads created 2006-12-25 15:50.