Click to See Complete Forum and Search --> : Perl loop question


vic102482
12-12-2002, 03:20 AM
Well I have two, first of all what is the default location of hte perl file that you are supposed to put in the first line of text?

#!/usr/bin/perl <-- is that it?


Second

While inside of a while loop like this:

(while(not a = b)){

$a = <STDIN>

}

can you do that?

Can you have information input from within a loop?

I tried and it doesnt seem to work.

Hena
12-12-2002, 03:36 AM
#!/usr/bin/perl
This tells to the computer that this file can be started with program in "/usr/bin/perl. Similarly "#!/bin/sh" tells that its a shell script using "/bin/sh".

Loop. Simple version should be similar to:
$b="";
while ($a ne $b)
{ $a=readline;
chomp $a;
push (@array,$a) unless ($a ne $b);
}
All the lines that are taken in are stored into array "@array". Changing $b changes the value that stops the input. Currently thats empty line.

vic102482
12-12-2002, 09:23 AM
The thing I wanted to know was, is i possible to prompt someone for input while you are in the middle of a while statement.



Like

Bah bah bah
while(b=0)
{
"please enter in correct variable";

$b = <STDIN>

if ($b eq 'y')

blah blah blah}

Is it possible to throw somehting in a while loop prompting for input? I treid it and I couldnt get it to work.

Hena
12-12-2002, 09:53 AM
The "$a=readline" promts one line from user. If you want to promt until certain value is gained (in here number, unless my pattern matching fails :p).
$a=readline;
chomp $a;
while ($a=~m/^[\d-.]?\d+$/)
{ print "Not number, give again:\n";
$a=readline;
chomp;
}

The pattern matching can ofcourse be changed to wanted version. Or use "eq" or "ne" for example.

If you want to include promt inside while() itself not inside while block block
while($a=readline)
might work. If it works similar thing should work with all functions that return something.

bwkaz
12-12-2002, 10:13 AM
To actually print your prompt, I am pretty sure Perl lets you do something like the following:

echo "please enter in correct variable";

Stuka
12-13-2002, 05:09 PM
<edit>Missed a post in the middle there, so this reply was silly.</edit>