RE: Extreme beginner question on reading lines from a file.



Hrm,
I am confused then:)
I have this as a file I am using right now!

while (<FILEIN>) {
my @data = split;
next unless @data == 3;
next if grep (/[^0-9.-]/, @data);
printf FILEOUT "X%s Y%s\n", $data[0], $data[1];
printf FILEOUT "G01 Z[%s+DPad] F[PFRate]\n", $data[2];
printf FILEOUT "[DrillZVal = %s]\n", $data[2];
print FILEOUT "M98PPECK.SUBL1\n";
print FILEOUT "G90\n";
print FILEOUT "G00 Z[CPlane]\n"
}

How come this works perfectly, it reads in each line separated 3-tuple of coordinates and processes them perfectly? I also am just learning, so I am not sure why I need to do more?
Thanks!
jlc

-----Original Message-----
From: Gary Stainburn [mailto:gary.stainburn@xxxxxxxxxxxxxx]
Sent: Wednesday, July 18, 2007 10:15 AM
To: beginners@xxxxxxxx
Subject: Re: Extreme beginner question on reading lines from a file.

On Wednesday 18 July 2007 17:08, Joseph L. Casale wrote:
Is that true?
I use while loops and they cycle through each line without me keeping
track? jlc

Yeah, it's true. Why would you want to keep track?
(you can BTW, look at perldoc perlvar for $.)

in my code, the 1st line is written into $prev. This code will fail if there's
only one line in the input file as the look would never be entered.

The ($this=<MYFILE>) reads from MYFILE putting the line into $this. The whole
expression then returns the result. If it's worked it returns true and the
loop is executed. If it fails it returns false and the loop exits.

Loosely speaking anyway

Gary


-----Original Message-----
[snip]
Hi Ayesha

you need to only read once per line, but remember the last one.

$prev=<MYFILE>
while ($this=<MYFILE>) {
print "$prev and $this\n";
$prev=$this;
}


--
Gary Stainburn

This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/






.



Relevant Pages

  • Re: Extreme beginner question on reading lines from a file.
    ... Yeah, it's true. ... the 1st line is written into $prev. ... This code will fail if there's ... If it fails it returns false and the loop exits. ...
    (perl.beginners)
  • Re: processing a sequence
    ... (loop for sublist = (collect-sublist) ... return accum if not element = list.first ... if prev and element < prev ... James still didn?t show his Ruby one-liner that outperforms the Lisp ...
    (comp.lang.lisp)
  • Re: File Closing Problem in 2.3 and 2.4, Not in 2.5
    ... It is very unlikely that there is a bug in Python where it would fail to ... you can use Sysinternal's process explorer. ... Put a print statement in the block that is supposed to close ... they may be out of "the loop" you thought that they were in. ...
    (comp.lang.python)
  • Re: Proof... ( After careful thought )
    ... These fail by producing sequences that ... end in a non-trivial loop. ... a power of 3, this is the _only_ positive loop. ...
    (sci.math)
  • Re: copy error
    ... each time through loop ... You are inspecting $! ... along the line, something that "copy" does is failing with that message, ... but the "copy" itself does not fail, ...
    (comp.lang.perl.misc)