Greeting I have a program take in a file and lets you add stuff to it after I add some words to it i want to count all the words in the file... I am having trouble with that ...here is my code so far. I looked up stuff online but it doesnt seem to work.

#!usr/bin/perl


print "Opening file attempt\n";
open (FILE, "+<@ARGV") or die ("Unable to open file exiting\n");
print "Printing current data in file:\n";
print while (<FILE>);
print "\nType something to add to file, press Enter when done\n";
$moredata=<STDIN>;
print (FILE $moredata);
close(FILE) or die ("Can't close this?!!?!?\n");


print "Opening file attempt\n";
open (NFILE, "@ARGV") or die ("Unable to open file exiting\n");
@array = <NFILE>;
close(NFILE) or die ("Can't close this?!!?!?\n");

foreach $string (@array){
$blah=shift($string);
$blah=split(/\W+/);
$count+=blah;
print $count;
}

Any help would be very appreciated...

Dani AI

Generated

Short recap: was trying to append to a file and then count words. 's split-based hint was enough to get a quick fix, and correctly pointed out that "what counts as a word" matters. Below is a more robust, memory-friendly pattern and a few practical notes that avoid the common pitfalls in the thread.

A streaming approach that counts words as you read avoids storing the whole file and handles most common cases with a single regex match per line:

open my $fh, '<', $ARGV[0] or die "Can't open '$ARGV[0]': $!";
my $count = 0;
while (my $line = <$fh>) {
    $count += () = $line =~ /[A-Za-z]+(?:'[A-Za-z]+)?/g;
}
close $fh;
print "$count\n";

That pattern treats sequences of letters as words and accepts simple contractions (O'Reilly). If you want to count hyphenated words as one token, use a pattern like [A-Za-z]+(?:[-'][A-Za-z]+)*. If numbers, acronyms (F.B.I.), or mixed alphanumerics (49ers) should count, adjust the character classes to include digits and dots — choose the definition that matches your needs and document it.

Practical tips drawn from the thread:

  • Prefer lexical filehandles and three-argument open (safer than the old style).
  • If you append before counting, open for append separately (or seek to end) so reads and writes don't collide.
  • If you use split, filter out empty fields (leading/trailing punctuation can produce empty strings).
  • Print the final count once (avoid printing inside the loop unless you need progress).
  • For deeper regex guidance refer to Perl’s docs on regexes and open.

Further reading: Perl regular expressions and open() function. The "Perl Cookbook" (mentioned by ) is also a good practical reference.

Recommended Answers

All 6 Replies

Is this school/class/course work? Be more specific about what you are having trouble with instead of just posting your code and saying "its not working" or "I am having trouble".

This whole block of code is just totally wrong for various reasons:

foreach $string (@array){
$blah=shift($string);
$blah=split(/\W+/);
$count+=blah;
print $count;
}

Add this line to the top of your script:

use warnings;

actually its not for anything....im doing it to learn and doing excercises out of a book from the library and it doesnt have the answers... and its been bugging me on why i cant get it

try this, assumes @array is populated correctly:

foreach $string (@array){
   @blah = split(/\W+/,$string);
   $count += @blah;
   print $count;
}

What is the title of the book?

ahh thanks! I got it to work now..... i had to manipulate my code a little bit but it works...

Whats the title of the book you are using from the Library?

One thing you have to be careful of is defining what you mean by 'word'. If your data contains only English 'words', what will your regex do with things like O'Reily, 49ers, street-wise, F.B.I. and so on. One of my most-used books is the 'Perl Cookbook' by Tom Christiansen & Nathan Torkington. This situation is covered in there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.