For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2007 > Re: Question abount counting the total number of lines and put to an variable?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Question abount counting the total number of lines and put to an variable?
Dr.Ruud

2007-11-29, 10:00 pm

Marco schreef:

> I have a question about how to count the total of line that shows on
> the screen. Here below is my code..
>
> open (MYFILE, '123.txt');
> while (<MYFILE> ) {
> if ($_ =~ /^User:/) {
> print $_;
> }
> }
> close (MYFILE);
>
> then it will shows the following on the screen,
>
> User: ABC
> User: DEF
>
> Is there any way that I can get the total of lines that show on the
> screen and put to a variable? " Thank you..



#!/usr/bin/perl
use strict;
use warnings;

my $fn = '123.txt';
{ open my $fh, "<", $fn or die "\n${fn}: $!";
print "\n${fn}\n";
print "-"x length($fn), "\n";
my $i = 0;
while (<$fh> ) {
/^User:/ and ++$i and print;
}
print "\n$i lines\n";
}
__END__

--
Affijn, Ruud

"Gewoon is een tijger."
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com