[comp.lang.perl] Reading a line in perl

wnp@iiasa.AT (wolf paul) (09/25/90)

Well, I took the plunge and started to use perl.

One of my first projects is to write an accounting script for our
uucp usage.

The SYSLOG files, on which this is to be based, contain two lines
for each transmission, the first being for the message body, and 
the second for the envelope/command file. I therefore need to process
the file in terms of two lines at a time.

What I want to do is this:

	while ($line1 = <INFILE>)
	{
		$line2 = getline INFILE ;
		.
		.
		.
	}

but I cannot find a "getline" function. Is there a way, short of
writing my own getline function using getc?

Thanks!
-- 
Wolf N. Paul, IIASA, A - 2361 Laxenburg, Austria, Europe
PHONE: +43-2236-71521-465     FAX: +43-2236-71313      UUCP: uunet!iiasa.at!wnp
INTERNET: wnp%iiasa.at@uunet.uu.net      BITNET: tuvie!iiasa!wnp@awiuni01.BITNET
       * * * * Kurt Waldheim for President (of Mars, of course!) * * * *

merlyn@iwarp.intel.com (Randal Schwartz) (09/26/90)

In article <903@iiasa.UUCP>, wnp@iiasa (wolf paul) writes:
| Well, I took the plunge and started to use perl.

You may regret that someday. :-)

| The SYSLOG files, on which this is to be based, contain two lines
| for each transmission, the first being for the message body, and 
| the second for the envelope/command file. I therefore need to process
| the file in terms of two lines at a time.
| 
| What I want to do is this:
| 
| 	while ($line1 = <INFILE>)
| 	{
| 		$line2 = getline INFILE ;
| 		.
| 		.
| 		.
| 	}
| 
| but I cannot find a "getline" function. Is there a way, short of
| writing my own getline function using getc?

while ($line1 = <INFILE>) {
	if (eof) {
		# handle line1 but no line2 error here
		last; # dumps you out of the loop
	}
	$line2 = <INFILE>;
	...
}

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: "Welcome to Portland, Oregon, home of the California Raisins!"=/

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (09/26/90)

In article <903@iiasa.UUCP> wnp@iiasa.UUCP (wolf paul) writes:
: What I want to do is this:
: 
: 	while ($line1 = <INFILE>)
: 	{
: 		$line2 = getline INFILE ;
: 		.
: 		.
: 		.
: 	}
: 
: but I cannot find a "getline" function. Is there a way, short of
: writing my own getline function using getc?

Just say

    while ($line1 = <INFILE>)
    {
	$line2 = <INFILE> ;
	.
	.
	.
    }

The <INFILE> notation IS the getline function in Perl.

Depending on how you want to handle $line2 not existing, you might want
to say something like

    while ($line1 = <INFILE) {
	$line2 = <INFILE> || warn("Out of sync") && last;
	...

Larry