Advertisements

John <> wrote in comp.lang.perl.misc:
> I'd like to move down a file and assign each line to a different variable.

I'm pretty sure you don't really want to do that. How would you access the
last line of a file of 1234 lines? $one_thousand_two_hundred_thirty_fourth?

You *may* want to assign the lines to an array with each line in one
position.
> How can I skip/move forward a line within the following while loop?
>
> while (<FILE>) {
> first = $_;
> second = $_;
> third = $_;
> }

First off, you forgot the "$"s in front of your variables. A quick test
(perl -c) would have shown your error. If you post code, please make
sure it is syntactically correct.

Otherwise, the code doesn't do in the least what you want. It assigns
the *same* line (in $_) into $first, $second and $third. Then, in the
next iteration, it overwrites the values with the next line. You end
up with the last line of the file in all three variables.

Advertisements

Abigail <> wrote in comp.lang.perl.misc:
> John () wrote on MMMDCXCVI September MCMXCIII in
> <URL:news:NpRib.150645$>:
> ~~ I'd like to move down a file and assign each line to a different variable.
> ~~ How can I skip/move forward a line within the following while loop?
> ~~
> ~~ while (<FILE>) {
> ~~ first = $_;
> ~~ second = $_;
> ~~ third = $_;
> ~~ }
>
> It depend a bit on what you want to do with the last lines if the
> file doesn't have a multiple of three lines in the file, but I
> would do something like:
>
> { my $first = <FILE> err last;
> my $second = <FILE> err last;
> my $third = <FILE> err last;
>
> # Process lines here.
>
> redo;
> }

Ah... you probably understood the OPs intention better than I did
in another reply.

"Anno Siegel" <-berlin.de> wrote in message
news:bmgpm4$foe$-Berlin.DE...
> John <> wrote in comp.lang.perl.misc:
> > I'd like to move down a file and assign each line to a different
variable.
>
> I'm pretty sure you don't really want to do that. How would you access
the
> last line of a file of 1234 lines?
$one_thousand_two_hundred_thirty_fourth?
>
> You *may* want to assign the lines to an array with each line in one
> position.
>
> > How can I skip/move forward a line within the following while loop?
> >
> > while (<FILE>) {
> > first = $_;
> > second = $_;
> > third = $_;
> > }
>
> First off, you forgot the "$"s in front of your variables. A quick test
> (perl -c) would have shown your error. If you post code, please make
> sure it is syntactically correct.
>
> Otherwise, the code doesn't do in the least what you want. It assigns
> the *same* line (in $_) into $first, $second and $third. Then, in the
> next iteration, it overwrites the values with the next line. You end
> up with the last line of the file in all three variables.
>
> With an array the solution looks like this:
>
> my @list;
> while (<FILE>) {
> push @list, $_;
> }
>
> or just
>
> my @list = <FILE>;
>
> > # all below vars have different values
> > print $first;
> > print $second;
> > print $third;
>
> print $list[ $_] for 0 .. 2;
>
> Anno

Thanks Anno. I actually changed to an array solution and it works as I
needed it in a much neater way:
open(FILE, $file);
@lines = <FILE>;
close(FILE);

On Tue, 14 Oct 2003 11:50:05 GMT, "John" <> wrote:
>I'd like to move down a file and assign each line to a different variable.
>How can I skip/move forward a line within the following while loop?
>
>while (<FILE>) {
> first = $_;
> second = $_;
> third = $_;
>}
>
># all below vars have different values
>print $first;
>print $second;
>print $third;

On Tue, 14 Oct 2003 11:50:05 GMT
"John" <> wrote:
> I'd like to move down a file and assign each line to a different
> variable. How can I skip/move forward a line within the following
> while loop?
>
> while (<FILE>) {
> first = $_;
> second = $_;
> third = $_;
> }
>
> # all below vars have different values
> print $first;
> print $second;
> print $third;

--untested--
my @lines = <FILE>;
#access each ine as an element of the array
--untested--

-or-

use one of the _many_ Perl modules relating to files.

This question is a bit like "How do I print a formated line?". The
accepted/first method is to use 'printf'. However, depending on what
the end result is supposed to be and what else needs to be done and
how the output is _supposed_ to look, you could use 'format'. Or, you
could use 'sprintf' to format the string first, then use'print' to
print the line (now containing the formated string).

Try writing out what you want to do on paper _first_. The answer
_may_ just pop off the page at you.

You need to increment $count each time, or you're going to repeatedly
assign $_ and $first, and none of the others. Is it not possible for
you to test the solutions you propose?
> --untested--
> my @lines = <FILE>;
> #access each ine as an element of the array
> --untested--

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!