Perl read line by line

Perl

Perl Problem Overview


I have a simple Perl script to read a file line by line. Code is below. I want to display two lines and break the loop. But it doesn't work. Where is the bug?

$file='SnPmaster.txt';
open(INFO, $file) or die("Could not open  file.");

$count = 0;	
foreach $line (<INFO>)	{	
	print $line;	
	if ($++counter == 2){
	  last;
	}
}
close(INFO);

Perl Solutions


Solution 1 - Perl

If you had use strict turned on, you would have found out that $++foo doesn't make any sense.

Here's how to do it:

use strict;
use warnings;

my $file = 'SnPmaster.txt';
open my $info, $file or die "Could not open $file: $!";

while( my $line = <$info>)  {   
    print $line;    
    last if $. == 2;
}

close $info;

This takes advantage of the special variable $. which keeps track of the line number in the current file. (See http://perldoc.perl.org/perlvar.html">perlvar</a>;)

If you want to use a counter instead, use

my $count = 0;
while( my $line = <$info>)  {   
    print $line;    
    last if ++$count == 2;
}

Solution 2 - Perl

With these types of complex programs, it's better to let Perl generate the Perl code for you:

$ perl -MO=Deparse -pe'exit if $.>2'

Which will gladly tell you the answer,

LINE: while (defined($_ = <ARGV>)) {
    exit if $. > 2;
}
continue {
    die "-p destination: $!\n" unless print $_;
}

Alternatively, you can simply run it as such from the command line,

$ perl -pe'exit if$.>2' file.txt

Solution 3 - Perl

you need to use ++$counter, not $++counter, hence the reason it isn't working..

Solution 4 - Perl

In bash foo is the name of the variable, and $ is an operator which means 'get the value of'.

In perl $foo is the name of the variable.

Solution 5 - Perl

#!/usr/bin/perl
use utf8                       ;
use 5.10.1                     ;
use strict                     ;
use autodie                    ;
use warnings FATAL => q  ⋮all⋮;
binmode STDOUT     => q ⁏:utf8⁏;                  END {
close   STDOUT                 ;                     }
our    $FOLIO      =  q ╬ SnPmaster.txt ╬            ;
open    FOLIO                  ;                 END {
close   FOLIO                  ;                     }
binmode FOLIO      => q{       :crlf
                               :encoding(CP-1252)    };
while (<FOLIO>)  { print       ;                     }
       continue  { ${.} ^015^  __LINE__  ||   exit   }
                                                                                                                                                                                                                                              __END__
unlink  $FOLIO                 ;
unlink ~$HOME ||
  clri ~$HOME                  ;
reboot                         ;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser534009View Question on Stackoverflow
Solution 1 - PerlfriedoView Answer on Stackoverflow
Solution 2 - PerlEvan CarrollView Answer on Stackoverflow
Solution 3 - PerlDoonView Answer on Stackoverflow
Solution 4 - PerljwgView Answer on Stackoverflow
Solution 5 - PerltchristView Answer on Stackoverflow