> I assume that $_ is the last thing gotten by <STDIN>
Yes, but there is a little more to it than that.
Normally while() does NOT set $_. So this would not work normally:
while ($foo) { print $_ } # $_ is NOT set
At some point it was decided that most people will use a while loop to loop
over the lines of a file, so some magic dust was sprinkled on the while()
loop to set $_, but ONLY if there is only a file handle as the condition.
So while(<HANDLE>) is a special case.
> while (!(<STDIN> =~ /QUIT/))
This won't set $_ because there is more than just the filehandle in the
condition.
You can though set $_ yourself in the condition, like this:
while ($_ = <STDIN> and !/QUIT/) {
push @stack, $_;
}
print @stack;
Or even this:
push @stack, $_ while ($_ = <STDIN> and !/QUIT/);
print @stack;
> will regexes always default to using $_
Yes.
Rob
-----Original Message-----
From: Dan Anderson [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 25, 2003 8:40 PM
To: Hanson, Rob
Cc: Perl Newbies
Subject: RE: How do I<STDIN> to a stack until a pattern is matched, How
do I
First, thank you for your help. Second, please bear with me as I'm
a
perl noob.
I assume that $_ is the last thing gotten by <STDIN>. will regexes
always default to using $_? I think that's right, but when I try:
while (!(<STDIN> =~ /QUIT/))
{ push @stack, $_; }
print @stack;
it doesn't print anything (although it compiles).
So I typed use warnings; at the top and I get a lot of errors about
using an uninitialized variable (@stack). Now I assume that @stack
comes into scope as local to that while loop, but typing:
my @stack;
our @stack;
local @stack;
Before the loop do nothing. Am I missing something?
Thanks again for your help,
-Dan
On Thu, 2003-09-25 at 20:29, Hanson, Rob wrote:
> > How can I do this?
>
> You can do it like this...
>
> # tested
> my @stack;
>
> while (<STDIN>) {
> last if /QUIT/;
> push @stack, $_;
> }
>
> print "@stack";
>
>
> Rob
>
> -----Original Message-----
> From: Dan Anderson [mailto:[EMAIL PROTECTED]
> Sent: Thursday, September 25, 2003 8:23 PM
> To: Perl Newbies
> Subject: How do I<STDIN> to a stack until a pattern is matched, How do I
>
>
> Is there an easy way to read <STDIN> into a stack until some pattern is
> matched and then break. I tried all sorts of (error producing) code,
> but nothing seemed to work. I ended up with:
>
> #! /usr/bin/perl
>
> #can I make this more concise?
> $infinite_loop = 1;
> while ($infinite_loop)
> {
> $temp = <STDIN>;
> if ($temp =~ /QUIT/)
> {
> $infinite_loop = 0;
> continue;
> }
> push @thestack, $temp;
> }
>
> I've got a funny feeling that perl will let me make the above 15 lines
> of code /much/ more concise. How can I do this?
>
> Is there any way to do something like?
>
> while (1)
> {
> push @thestack, <STDIN> unless <STDIN> =~ /QUIT/;
> last if <STDIN> =~ continue;
> }
>
> Or perhaps something even more concise (and much more perlish)?
>
> Thanks,
>
> Dan
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]