perl references in a while loop

while (0..25) {



until ($x < 25) {
print "hello::","\n";

$in = STDIN;

print ${$d[$in]};

goto lable;


## now this should print 1 when i type 1
## and 2 when I type 2 at
## and 3 when I type 3 instead it types 25
## for 1 to 25 typed in

## plese help


Whatever it is you want it to do, you're doing it all wrong.

The range in the while loop always returns true, which means that the while loop is never exited.

until is checked to see if the statement is false and so is equivalent to

while ($x >= 25)

the lable: and goto lable; is an additional loop inside the until loop. Since the $x variable is never decremented inside the until loop, it would never be exited, so the goto loop is superfluous.

Assuming that STDIN is actually enclosed in angle brackets, your main problem, however, is that you are assigning a reference to $x to all of the @d indexes from 0 to 24. (If you type in 25, you won't get 25, you'll get undefined.) A reference means that you get what $x is when you dereference the array slot and not what it was when you assigned it., that's what you'll get for every index of @d from 1 to 24. Since the last thing $x was assigned was 25

Probably the code you want is:

my @d;

for my $x (0..25) {
$d[$x] = $x;

while (1) {
my $in = <STDIN>;
print "hello::", $d[$in], "\n";

Leave a comment

About Mark

user-pic while (0..25) { $x++; $d[$t++]=\$x; until ($x lable:print "hello","\n"; $in = ; print ${$d[$in]}; goto lable; }} ## why wont this work !!!... ???