Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple external file line counter

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi everyone,
I need some help here.
I'm trying to create a simple script that performs two tasks.

1. counts the number of lines in a file.
2. displays the total count in a web page.

So, say pet.db contains;
cat
dog

I want to display;
Pets in database: 2

This is the script I,ve got so far;

#!/usr/local/bin/perl
# =====================
# ---------------------
# Record Counter V1.0
# ---------------------
# =====================

my $curr = 1;
open (CURR, "pet.db") or die $!;
$curr++ while (<CURR>);
close CURR;
print $curr;
exit;
}


I use this SSI, <!--#exec cgi=&quot;counter.cgi&quot;--> to try and display $curr in a web page.


This should work. But it doesn't !
Is there something I've missed?
Any help welcome.

Thank you.




 
I think I know this works:
Code:
#!/usr/bin/perl

$dbfile = &quot;pet.db&quot;;
$counter = 0;
open(DATA,&quot;$dbfile&quot;) || die &quot;Unable to open $dbfile&quot;;
@line=<DATA>;
close(DATA);
foreach $line(@lines)
 {
 $line =~ s/\n//g;
 $counter++;
 }
print &quot;Pets in database: $counter&quot;;
exit;

Probably not the best way.... but I'm sure this would work.
 
Here's one:

In your .shtml file, use the #include virtual directive instead of #exec.
Code:
<!--#include virtual=&quot;/cgi-bin/count.cgi&quot;-->
And count.cgi can be something as simple as:
Code:
#!/usr/bin/perl
print &quot;Content-type: text/html\r\n\r\n&quot;;
open(FH,&quot;pet.db&quot;);
print scalar @_ while @_ = <FH>;
close FH;
Be sure to have the Content-type line in there, otherwise you'll get a header error.

Hope this helps,

brendanc@icehouse.net
 
I knew I'd find some way to screw it up! :)

sophisticate... I might be able to use this for a script I'm writing now. Is there an easy way to modify what you posted above to return a variable holding the number of lines?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top