Created
November 28, 2011 08:36
-
-
Save numberwhun/1399635 to your computer and use it in GitHub Desktop.
Count lines in a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
###################################################################################### | |
# Script: count.pl | |
# Author: Jefferson Kirkland | |
# | |
# Description: | |
# The purpose of this subroutine is to take a file name as an input parameter, and | |
# then return to the user the count of the number of lines in that file. | |
# | |
# USAGE: line_count(filename); | |
# | |
# where "filename" is the name of the file (using absolute path if not in the same | |
# directory as the script being run. | |
###################################################################################### | |
sub line_count{ | |
############################################################ | |
# Put the file name into a variable to be passed elsewhere | |
############################################################ | |
my ($file) = $_[0]; | |
############################################################ | |
# Open the file for reading so we can count the lines | |
############################################################ | |
open(FILE, "< $file"); | |
############################################################ | |
# Initialize the counter to zero (0) | |
############################################################ | |
my $count = 0; | |
############################################################ | |
# Count the lines in the file. This is pretty non-trivial. | |
# All it does is increment the counter once for each line | |
# in the file. | |
############################################################ | |
while(<FILE>) | |
{ | |
$count++; | |
} | |
############################################################ | |
# Close the file as counting is completed. | |
############################################################ | |
close(FILE); | |
############################################################ | |
# Print the count of the number of lines in the file. | |
############################################################ | |
print("$count \n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment