Re: Breaking out of STDIN Question

2002-09-28 Thread John W. Krahn

Grant Hansen wrote:
> 
> Can someone tell me what is wrong with this.
> 
> Upon entering a 0 I want to break out of the loop and continue processing the
> data in @data.
> 
> $quit = 0;
> while (! $quit) {
> chomp(@data = );;
> if ($data == 0) {
> $quit = 1;
> }}


If you want to stop input based on what the user entered on the command
line then you have to read ONE line at a time and not use an array for
input.  BTW your loop will only read numbers and not strings because the
test '== 0' will evaluate strings in a numerical context which are
always equal to 0.

while ( my $input =  ) {
chomp $input;
last if $input == 0;
push @data, $input;
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Breaking out of STDIN Question

2002-09-29 Thread Jenda Krynicky

From: "John W. Krahn" <[EMAIL PROTECTED]>
> Grant Hansen wrote:
> > 
> > Can someone tell me what is wrong with this.
> > 
> > Upon entering a 0 I want to break out of the loop and continue
> > processing the data in @data.
> > 
> > $quit = 0;
> > while (! $quit) {
> > chomp(@data = );;
> > if ($data == 0) {
> > $quit = 1;
> > }}
> 
> 
> If you want to stop input based on what the user entered on the
> command line then you have to read ONE line at a time and not use an
> array for input.  BTW your loop will only read numbers and not strings
> because the test '== 0' will evaluate strings in a numerical context
> which are always equal to 0.

Unless the string starts with a number that is not equal to zero :-)

That is this loop

> while ( my $input =  ) {
> chomp $input;
> last if $input == 0;
> push @data, $input;
> }

will read lines as long as they all start with a non zero number.
Therefore the @data may the contain things like
1sdgdfsg
59 this is nonsense
-1 bellow zero

Jenda

=== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
--- me


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Breaking out of STDIN Question

2002-09-30 Thread david

Jenda Krynicky wrote:

>> 
>> If you want to stop input based on what the user entered on the
>> command line then you have to read ONE line at a time and not use an
>> array for input.  BTW your loop will only read numbers and not strings
>> because the test '== 0' will evaluate strings in a numerical context
>> which are always equal to 0.
> 
> Unless the string starts with a number that is not equal to zero :-)
> 

true but if you have warning enable (which i think is a good idea), Perl 
will warn you about this even the string starts with a number but doesn't 
contain purely number. so if you want to avoid those warning, you can 
disable warning (which is not recommanded) or you have to enter a pure 
number. :-)

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]