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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deriving # of lines from a file... Simple sintax problem.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
HEy.

Does anyone know the command that sends back the number of lines from a file.

So like xxx(filename)?

That would be super helpful.

Thanks.
-NeXius
 

Easy way to do it is,

open(file, &quot;<file.txt&quot;);
@all=<file>;
close(file);
$line_number=@all;

If you print &quot;$line_number&quot;; you'll have to total amout of lines in that file.

Sleuth
 
Or, another way to do it......
If you are on a UNIX box, you can use the Word Count command in backticks.

$num_words = `wc -l $filename`;

That might also be possible on a Win box????? I can't remember if the Cyg-win tools have a version of 'wc'. If they do, you could get a copy of Cyg-win and use the same syntax.


keep the rudder amid ship and beware the odd typo
 
Similar to sleuth's answer:

[tt]

open(FILE,&quot;file.txt&quot;) || die(&quot;failed to open file: $!&quot;);
@data = <FILE>;
close(FILE);

foreach (@data) {
$i++;
}
print $i;

[/tt]

I suppose thats a longer way, but just thought that I would like to throw it in.


-Vic

vic cherubini
malice365@hotmail.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top