Brauche schnell hilfe bei perl!!!
Posted: 06. Jul 2003 15:25
Hallo,
kann mir bitte jemand schnell helfen, wiso geht das nicht?
Ich will im Array für den jeweiligen turm die scheiben haben, also das Array für turm 1 sieht am anfang z.B. so aus @turm1[1,2,3,4].
Jetzt will ich das erste element nehmen und z.B. auf den turm 2 legen, dann müssten die arrays so aussehen: @turm1[2,3,4] @turm2<font size="1">;
die übergabe klappt aber irgendwie nicht.
Danke im voraus.
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
1 #! /usr/bin/perl -w
2
3 my $count = 0;
4 my $num = 1;
5 my $n = 1;
6 my $x = 1;
7 my @tower1;
8 my @tower2;
9 my @tower3;
10
11 ($#ARGV == 0) or die "Syntax: $0 N
";
12 my $N = int($ARGV[0]);
13 ($N > 0) or die "$0: Nicht erlaubte Anzahl von Scheiben
";
14
15
16 #Array füllen#
17 while ($num<=$N) {
18
19 $tower1[$n] = $num;
20 $n++;
21 $num++;
22 }
23
24 hanoi($N, right, left, center, @tower1, @tower2, @tower3);
25
26 sub hanoi {
27 my ($n, $to, $from, $using, @to, @from, @using) = @_;
28
29 $count = $count+1;
30
31 if ($n == 1) {
32 print "move $from --> $to aaaa
"; # trivialer Fall
33
34 $temp = shift @from;
35 unshift @to, $temp;
36
37
38 } elsif ($n > 1) {
39 hanoi($n-1, $using, $from, $to, @using, @from, @to); # (*)
40 print "move $from --> $to xxxx
"; # (**)
41
42 $temp = shift @from;
43 unshift @to, $temp;
44
45 hanoi($n-1, $to, $using, $from, @to, @using, @from); # (***)
46 }
47 }
48
49 print"
-->$count
";
</font><hr></pre></blockquote>
kann mir bitte jemand schnell helfen, wiso geht das nicht?
Ich will im Array für den jeweiligen turm die scheiben haben, also das Array für turm 1 sieht am anfang z.B. so aus @turm1[1,2,3,4].
Jetzt will ich das erste element nehmen und z.B. auf den turm 2 legen, dann müssten die arrays so aussehen: @turm1[2,3,4] @turm2<font size="1">;
die übergabe klappt aber irgendwie nicht.
Danke im voraus.
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
1 #! /usr/bin/perl -w
2
3 my $count = 0;
4 my $num = 1;
5 my $n = 1;
6 my $x = 1;
7 my @tower1;
8 my @tower2;
9 my @tower3;
10
11 ($#ARGV == 0) or die "Syntax: $0 N
";
12 my $N = int($ARGV[0]);
13 ($N > 0) or die "$0: Nicht erlaubte Anzahl von Scheiben
";
14
15
16 #Array füllen#
17 while ($num<=$N) {
18
19 $tower1[$n] = $num;
20 $n++;
21 $num++;
22 }
23
24 hanoi($N, right, left, center, @tower1, @tower2, @tower3);
25
26 sub hanoi {
27 my ($n, $to, $from, $using, @to, @from, @using) = @_;
28
29 $count = $count+1;
30
31 if ($n == 1) {
32 print "move $from --> $to aaaa
"; # trivialer Fall
33
34 $temp = shift @from;
35 unshift @to, $temp;
36
37
38 } elsif ($n > 1) {
39 hanoi($n-1, $using, $from, $to, @using, @from, @to); # (*)
40 print "move $from --> $to xxxx
"; # (**)
41
42 $temp = shift @from;
43 unshift @to, $temp;
44
45 hanoi($n-1, $to, $using, $from, @to, @using, @from); # (***)
46 }
47 }
48
49 print"
-->$count
";
</font><hr></pre></blockquote>