Thread Nicht numerische Variablen in Perl: Problem mit nicht numerischen Variablen (5 answers)
Opened by Gast at 2006-12-25 15:50

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-->

View full thread Nicht numerische Variablen in Perl: Problem mit nicht numerischen Variablen