Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How does one move down a line in a file?

Reply
Thread Tools

How does one move down a line in a file?

 
 
John
Guest
Posts: n/a
 
      10-14-2003
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;


Thanks


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-14-2003
John <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-14-2003
Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> John ((E-Mail Removed)) wrote on MMMDCXCVI September MCMXCIII in
> <URL:news:NpRib.150645$(E-Mail Removed)>:
> ~~ 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.

The "err" operator looks interesting. How is it specified?

Anno
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      10-14-2003

"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:bmgpm4$foe$(E-Mail Removed)-Berlin.DE...
> John <(E-Mail Removed)> 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);






 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-14-2003
John <(E-Mail Removed)> wrote:

> How can I skip/move forward a line within the following while loop?



By simply reading from the file:


> while (<FILE>) {
> first = $_;
> second = $_;
> third = $_;


if ( want_to_skip_line() ) {
<FILE>; # read and discard line
}


> }



--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Helgi Briem
Guest
Posts: n/a
 
      10-14-2003
On Tue, 14 Oct 2003 11:50:05 GMT, "John" <(E-Mail Removed)> 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;


Don't do that. Use a hash instead:

#!perl
use warnings;
use strict;
my %line;
while (<DATA>)
{
$line{$.}= $_;
}

# Have a look at the contents of %line
for (sort { $a <=> $b } keys %line)
{
print "$_\t$line{$_}";
}
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-14-2003
John <(E-Mail Removed)> wrote:


[ snip full-quote. Please do not do that. ]


> open(FILE, $file);



You should always, yes *always*, check the return value from open():

open(FILE, $file) or die "could not open '$file' $!";


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-14-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Helgi Briem <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Don't do that. Use a hash instead:
>
> #!perl
> use warnings;
> use strict;
> my %line;
> while (<DATA>)
> {
> $line{$.}= $_;
> }
>
> # Have a look at the contents of %line
> for (sort { $a <=> $b } keys %line)
> {
> print "$_\t$line{$_}";
> }


You would do the above instead of

@line = <DATA>;

for ($i=0; $i<@line; $i++)
{
print "$i\t$line[$i]";
}

?

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP4v/jmPeouIeTNHoEQLQ4wCeNNGVhWyUvgPgSo2U6yhiYTogMo4An3 5P
4SOlnDyTHapWOcgjYeO+5wdD
=Tpwh
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-14-2003
Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel ((E-Mail Removed)-berlin.de) wrote on MMMDCXCVI
> September MCMXCIII in <URL:news:bmgq1q$foe$(E-Mail Removed)-Berlin.DE>:
> "" Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:


[...]

> "" > 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.
> ""
> "" The "err" operator looks interesting. How is it specified?
>
> The same as the perl6 operator. Avaiable as a patch against 5.8.1 on
> CPAN: authors/id/H/HM/HMBRAND/dor-5.8.1.diff.


Ah, very nice. Compiling...

Anno
--
$_ = 'Just a LISP hacker,';
$x = \ substr( $_, 6, 5);
$$x = 'nother Perl';
print
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-14-2003
Eric J. Roode <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Helgi Briem <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
> > Don't do that. Use a hash instead:
> >
> > #!perl
> > use warnings;
> > use strict;
> > my %line;
> > while (<DATA>)
> > {
> > $line{$.}= $_;
> > }
> >
> > # Have a look at the contents of %line
> > for (sort { $a <=> $b } keys %line)
> > {
> > print "$_\t$line{$_}";
> > }

>
> You would do the above instead of
>
> @line = <DATA>;
>
> for ($i=0; $i<@line; $i++)
> {
> print "$i\t$line[$i]";
> }


Why that instead of

print "$_\t$line[$_]" for 0 .. $#line;



Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Writing move constructors and move assignment Andrew Tomazos C++ 2 12-12-2011 01:45 PM
One line down, one to go + charges? T i m UK VOIP 27 09-15-2007 11:43 AM
I am trying to move spaces to a weblistbox and when I move them... Eduardo78 ASP .Net Web Controls 0 11-03-2005 06:06 PM
CSS aligning two things on one line with one left and one right news.frontiernet.net HTML 6 04-16-2004 02:44 AM



Advertisments