CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
HOw to count records?

 



DippinPete
Deleted

Jan 4, 2001, 9:25 AM

Post #1 of 6 (445 views)
HOw to count records? Can't Post

I've got a flat-file

h|p|lk|d|f|
3|4|5|6|7|
7|4|3|2|fg|

I'd like to have some perl that can count the number of records.

Anyone know how?

Pete



BigRich
Novice

Jan 4, 2001, 10:46 PM

Post #2 of 6 (436 views)
Re: HOw to count records? [In reply to] Can't Post

Here are a few simple examples.


#!perl -wT

print "Content-type: text/html\n\n";

use strict;

my $data_file = '/path/to/record_data.dat';

##################################
## Method 1 (Efficient)
##
## Opens the file and uses a "while" loop to increment the count.
##
my $cnt = 0;
open(FILE, "$data_file") or die "Could not open $data_file: $!";
while (<FILE>) {
$cnt++;
}
close(FILE);
print "Method 1 count is $cnt <p>";
##################################

##################################
## Method 2 (Less efficient)
##
## Opens the file.
## Reads in the contents of the file and assigns it to the @files array.
## uses $scalar = @array ($cnt2 = @files) to get the number of elements
## in @files and assigns the number to $cnt2.
##
open(FILE2, "$data_file") or die "Could not open $data_file: $!";
my @files = <FILE2>;
close(FILE2);
my $cnt2 = @files;
print "Method 2 count is $cnt2 <p>";
##################################

##################################
## Method 3 (Even less efficient)
##
## Opens the file.
## Reads in the contents of the file and assigns it to the @all_files array.
## Uses a "foreach" loop to increment the count for each element in @all_files.
##
open(FILE3, "$data_file") or die "Could not open $data_file: $!";
my @all_files = <FILE3>;
close(FILE3);
my $cnt3 = 0;
foreach (@files) {
$cnt3++;
}
print "Method 3 count is $cnt3 <p>";
##################################
__END__

BigRich



japhy
Enthusiast

Jan 5, 2001, 7:37 AM

Post #3 of 6 (428 views)
Re: HOw to count records? [In reply to] Can't Post

Modified -- thanks, Mortimer

Perl can do this for you almost magically:


Code
open FILE, $filename or die "can't read $filename: $!"; 
1 while scalar <FILE>;
print "There are $. lines in $filename\n";
close FILE;

My errors were using FH where I meant FILE, and closing the filehandle before using (or saving the value of) $.. Closing a filehandle resets $. to 0.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

(This post was edited by japhy on Jan 5, 2001, 12:44 PM)


Mortimer
journeyman

Jan 5, 2001, 1:37 PM

Post #4 of 6 (423 views)
Re: HOw to count records? [In reply to] Can't Post


Code
>open FILE, $filename or die "can't read $filename: $!"; 
>1 while scalar <FH>;
>close FILE;
>print "There are $. lines in $filename\n";

This only returns 0. And you're trying to read on a closed filehandle. But it still
returns 0 even if you change the filehandle.

This works:


Code
open(FILE,$filename)or die("Cannot open $filename for input: $!"); 
while(<FILE>){}print $.;
close(FILE);

Dave.
www.dmscripts.com - davemortimer@one.net.au



japhy
Enthusiast

Jan 5, 2001, 1:44 PM

Post #5 of 6 (422 views)
Re: HOw to count records? [In reply to] Can't Post

Thank you for the heads-up. I'm rarely so careless as to do that... posting code without trying it to make sure I'm not making a fool of myself.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Mortimer
journeyman

Jan 5, 2001, 2:56 PM

Post #6 of 6 (419 views)
Re: HOw to count records? [In reply to] Can't Post

>Thank you for the heads-up. I'm rarely so
>careless as to do that... posting code
>without trying it to make sure I'm not
>making a fool of myself.

You are one of the few that would never be regarded as a fool. I've learned lots of clever and correct Perl from you. I'll look forward to reading more of your posts so I can learn a lot more.

Cheers,

Dave.
www.dmscripts.com - davemortimer@one.net.au


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives