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:
Perl file handle

 



philmetz
Novice

Oct 5, 2008, 6:55 AM

Post #1 of 19 (2375 views)
Perl file handle Can't Post

Hey

I am trying to create a perl program that will count the number of words in a file, but am confused how I can do this.
Could someone help?

Thanks


shawnhcorey
Enthusiast


Oct 5, 2008, 9:14 AM

Post #2 of 19 (2372 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post


In Reply To
Hey

I am trying to create a perl program that will count the number of words in a file, but am confused how I can do this.
Could someone help?

Thanks


First decide what is meant by "words". Are they:

1. Anything separated by whitespace?
2. Letters only?
3. Letters, digits, and underscores?
4. Letters with embedded hyphens (as in English)?
5. Something else?

Once you decide, you can write the regular expression pattern that will find them in the input.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


philmetz
Novice

Oct 5, 2008, 9:25 AM

Post #3 of 19 (2370 views)
Re: [shawnhcorey] Perl file handle [In reply to] Can't Post

I have tried the following:

Code
#!/usr/bin/perl -w 
print "Welcome to the File Word Count program\n";

print "Enter the name of the file to open\n";
$inFileName = <STDIN>;
$sum = 0;
open (FILE, "<$inFileName") or die $!;

for (1..40)
{
my $char = getc FILE;
print $char;
if ($char eq " ")
{
$sum = $sum + 1;
}
}
print $sum."\n";
close (FILE) or die $!;


But as you can see its a for loop, i tried while (<FILE>)
but that didnt work?


shawnhcorey
Enthusiast


Oct 5, 2008, 9:47 AM

Post #4 of 19 (2368 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post


In Reply To
I have tried the following:

Code
#!/usr/bin/perl -w 
print "Welcome to the File Word Count program\n";

print "Enter the name of the file to open\n";
$inFileName = <STDIN>;
$sum = 0;
open (FILE, "<$inFileName") or die $!;

for (1..40)
{
my $char = getc FILE;
print $char;
if ($char eq " ")
{
$sum = $sum + 1;
}
}
print $sum."\n";
close (FILE) or die $!;


But as you can see its a for loop, i tried while (<FILE>)
but that didnt work?


You have it set up so that the file name is coming from STDIN. Do you want this or do you want the input data to come from STDIN?

If you use Perl's spaceship operator '<>', it will be assign to STDIN if the are no command-line arguments, or to each file given in the command line.


Code
#!/usr/bin/perl 

use strict;
use warnings;

# Copy input to STDOUT

while( <> ){
print;
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


philmetz
Novice

Oct 5, 2008, 9:56 AM

Post #5 of 19 (2367 views)
Re: [shawnhcorey] Perl file handle [In reply to] Can't Post

Didnt i do that with <FILE> or is it without the FILE?
The file is to come from STDIN
What do the <> means?


(This post was edited by philmetz on Oct 5, 2008, 10:24 AM)


shawnhcorey
Enthusiast


Oct 5, 2008, 10:41 AM

Post #6 of 19 (2361 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post


In Reply To
Didnt i do that with <FILE> or is it without the FILE?
The file is to come from STDIN
What do the <> means?


<> is a special case of <FILE>

With <FILE>, the input comes from only that file handle. With <>, input comes from STDIN if there are no command-line arguments. If there are command-line arguments, each is considered to be a file, it is opened in its turn and becomes the input.


while( <FILE> ) will work. It assigns each line of input to the variable $_. To assign it to another use: while( my $line = <FILE> )

You can then use $line inside the while loop.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


philmetz
Novice

Oct 5, 2008, 10:46 AM

Post #7 of 19 (2360 views)
Re: [shawnhcorey] Perl file handle [In reply to] Can't Post

I have tried this:

Code
#!/usr/bin/perl -w 
print "Welcome to the File Word Count program\n";

print "Enter the name of the file to open\n";
chomp( $inFileName = <STDIN> );

$sum = 0;

open (FILE, "<$inFileName") or die $!;

while( my $line = <FILE> )
{
my $char = getc FILE;
print $char;
if ($char eq " ")
{
$sum = $sum + 1;
}
}
print $sum."\n";
close (FILE) or die $!;


But i get the error:

Code
[13818160@lab219-02 wk8]$ perl wordCount.pl 
Welcome to the File Word Count program
Enter the name of the file to open
words.pl
Use of uninitialized value in print at wordCount.pl line 14, <FILE> line 1.
Use of uninitialized value in string eq at wordCount.pl line 15, <FILE> line 1.
0


Whats wrong?

[also what does the 'my' in front of the variable mean?]


shawnhcorey
Enthusiast


Oct 5, 2008, 10:56 AM

Post #8 of 19 (2359 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post


In Reply To
I have tried this:

Code
#!/usr/bin/perl -w 
print "Welcome to the File Word Count program\n";

print "Enter the name of the file to open\n";
chomp( $inFileName = <STDIN> );

$sum = 0;

open (FILE, "<$inFileName") or die $!;

while( my $line = <FILE> )
{
my $char = getc FILE;
print $char;
if ($char eq " ")
{
$sum = $sum + 1;
}
}
print $sum."\n";
close (FILE) or die $!;


But i get the error:

Code
[13818160@lab219-02 wk8]$ perl wordCount.pl 
Welcome to the File Word Count program
Enter the name of the file to open
words.pl
Use of uninitialized value in print at wordCount.pl line 14, <FILE> line 1.
Use of uninitialized value in string eq at wordCount.pl line 15, <FILE> line 1.
0


Whats wrong?

[also what does the 'my' in front of the variable mean?]


The interior of your loop does not count words, it counts spaces after the first line.

'my' sets the scope of the variable that follows it to the block or file it is contained in. This is called lexical scoping.

Try this:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my %Words = ();

while( <> ){
for my $word ( split /\s+/, $_ ){
$Words{$word} ++;
}
}

for my $word ( sort keys %Words ){
printf "%-20s %5d\n", $word, $Words{$word};
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


philmetz
Novice

Oct 5, 2008, 10:58 AM

Post #9 of 19 (2358 views)
Re: [shawnhcorey] Perl file handle [In reply to] Can't Post

By setting the scope what do you mean?

Well my way of counting words should work as well and it does in the for loop but not in the while loop which is what i cant seem to get working. before i try your idea, i want to figure out whats wrong with mine?

also could you please explain this bit:

Code
..... 
for my $word ( split /\s+/, $_ ){
....



(This post was edited by philmetz on Oct 5, 2008, 11:06 AM)


maestria
Novice


Oct 5, 2008, 9:45 PM

Post #10 of 19 (2331 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post

Here is my version. Give the file name as the argument for the script.
Ex: perl filename.pl myfile.txt

#!/usr/bin/perl -l
while(<>){
@arr=split(/ /);
foreach (@arr){
$count++;
}
}
print $count;

------------------------------------------------------------------
If some one can do it, we can do it better
http://maestriatech.com
perl@maestriatech.com
--------------------------------------------------------------------


philmetz
Novice

Oct 5, 2008, 10:06 PM

Post #11 of 19 (2327 views)
Re: [maestria] Perl file handle [In reply to] Can't Post

what is the "/ /" for?
what does it mean?


maestria
Novice


Oct 5, 2008, 10:15 PM

Post #12 of 19 (2324 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post

/ / means split the $_ that comes from the above while loop,
ie split each line of the file when a space occurs.

Just go ahead and learn a bit more about $_ and split. you will understand better :)

------------------------------------------------------------------
If some one can do it, we can do it better
http://maestriatech.com
perl@maestriatech.com
--------------------------------------------------------------------


philmetz
Novice

Oct 5, 2008, 10:24 PM

Post #13 of 19 (2320 views)
Re: [maestria] Perl file handle [In reply to] Can't Post

maybe asking a bit too much but do you got a good tutorial for that?


maestria
Novice


Oct 5, 2008, 10:58 PM

Post #14 of 19 (2317 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post

For your better understanding, i can rewrite the script for your readability.
There is no hidden variables used in this script.

#!/usr/bin/perl -l
$filename=shift;
open(FILE,"$filename");
while($line=<FILE>){
@arr=split(/ /,$line);
foreach (@arr){
$count++;
}
}
print $count;

------------------------------------------------------------------
If some one can do it, we can do it better
http://maestriatech.com
perl@maestriatech.com
--------------------------------------------------------------------


philmetz
Novice

Oct 5, 2008, 11:04 PM

Post #15 of 19 (2316 views)
Re: [maestria] Perl file handle [In reply to] Can't Post

Ok yep beginning to make sense, thanks
Just one question: Whats the extra '{' for?


maestria
Novice


Oct 5, 2008, 11:07 PM

Post #16 of 19 (2315 views)
Re: [philmetz] Perl file handle [In reply to] Can't Post

Open the script in Vi editor,
Place the control over the } you have doubt and then press Shift+5
it will show you the other brace that it matches.

------------------------------------------------------------------
If some one can do it, we can do it better
http://maestriatech.com
perl@maestriatech.com
--------------------------------------------------------------------


philmetz
Novice

Oct 5, 2008, 11:12 PM

Post #17 of 19 (2312 views)
Re: [maestria] Perl file handle [In reply to] Can't Post

Ah yep didnt see the for loop one, thanks


FishMonger
Veteran / Moderator

Oct 6, 2008, 9:19 AM

Post #18 of 19 (2280 views)
Re: [maestria] Perl file handle [In reply to] Can't Post


In Reply To
Here is my version. Give the file name as the argument for the script.
Ex: perl filename.pl myfile.txt

#!/usr/bin/perl -l
while(<>){
@arr=split(/ /);
foreach (@arr){
$count++;
}
}
print $count;

How about showing beginners how to write quality code?


Code
#!/usr/bin/perl 

use warnings;
use strict;

die "Filename not passed\n" unless @ARGV;

chomp(my $inFileName = shift);

# I'd need to do a benchmark test, but this is probably the most efficient.
my $words = (split /\s+/, `wc $inFileName`)[2];
print "Words:$words\n";

# However, this one doesn't use an external utility.
my $count;
open my $FILE, '<', $inFileName or die "can't open '$inFileName' $!";
while ( <$FILE> ) {
$count++ for split;
}
print $count,$/;


[edit]
In my test, the wc command extracted the count of words, lines, and characters. When I posted, I simply altered the array slice to return only the word count. The command itself could be altered accordingly, if desired.[/edit]


(This post was edited by FishMonger on Oct 6, 2008, 9:27 AM)


KevinR
Veteran


Oct 6, 2008, 10:48 AM

Post #19 of 19 (2271 views)
Re: [FishMonger] Perl file handle [In reply to] Can't Post

I liked this one:


Quote
There is no hidden variables used in this script.


I didn't know perl even had hidden variables. Maybe he meant something else though. Crazy
-------------------------------------------------

 
 


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

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