Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Why does my variable keep changing?!! Message-ID: <1990Nov14.222140.22741@iwarp.intel.com> Date: 14 Nov 90 22:21:40 GMT References: <27962@sequoia.execu.com> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 54 In-Reply-To: painter@sequoia.execu.com (Tom Painter) In article <27962@sequoia.execu.com>, painter@sequoia (Tom Painter) writes: | In the following script, I trying to strip all lines before the first | blankline from stdin. In that vein, I set a flag ($header) to "Y" | and change it to "N" after it hits the blank line. So far it works. | However, when I return to the top of the while loop, $header has been changed | back to "Y". The "N" should last for the rest the time, I never want it | changed back. [...] | #! /usr/bin/perl | | $header = "Y"; | | while (<>) { | print $header; | if ($header = "Y"){ | if (/^$/){ | $header = "N"; | } | next; | } | if (grep(/SOME STRING/,$_)){ | printf " \n"; | } | print; | } The line beginning "if ($header ..." needs a "eq", not a "=". Also, I've never seen the grep operator used for a single entry (not even in a JAPH :-). Simply /SOME STRING/ would do it. Here's something closer to what you want: #!/usr/bin/perl while (<>) { $header = /^$/..eof ? "N" : "Y"; print $header; print "\f\n" if /SOME STRING/; print; } Some explanation on that first line: the construct "/^$/..eof" is true from the first blank line to the end of the file. This is a handy idiom, duplicating your logic. More often, you'll see something like "1../^$/", which is true from the first line of the file to the first blank line (not quite the same thing... think of the value *at* the blank line). print "Just another Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel put the 'backward' in 'backward compatible'..."=========/