Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3272552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:50:16+00:00 2026-05-17T18:50:16+00:00

I’ve got a question in my test: What is wrong with program that counts

  • 0

I’ve got a question in my test:

What is wrong with program that counts number of lines and words in file?

open F, $ARGV[0] || die $!;
my @lines = <F>;
my @words = map {split /\s/} @lines;
printf "%8d %8d\n", scalar(@lines), scalar(@words);
close(F); 

My conjectures are:

  1. If file does not exist, program won’t tell us about that.
  2. If there are punctuation signs in file, program will count them, for example, in

    abc cba
    , , ,dce
    

    will be five word, but on the other hand wc outputs the same result, so it might be considered as correct behavior.

  3. If F is a large file, it might be better to iterate over lines and not to dump it into lines array.

Do you have any less trivial ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:50:17+00:00Added an answer on May 17, 2026 at 6:50 pm

    On the first line, you have a precedence problem:

    open F, $ARGV[0] || die $!;
    

    is the same as

    open F, ($ARGV[0] || die $!);
    

    which means the die is executed if the filename is false, not if the open fails. You wanted to say

    open(F, $ARGV[0]) || die $!;
    

    or

    open F, $ARGV[0] or die $!;
    

    Also, you should be using the 3 argument form of open, in case $ARGV[0] contains characters that mean something to open.

    open F, '<', $ARGV[0] or die $!;
    

    On a different note, splitting on /\s/ means that you get a “word” between consecutive whitespace characters. You probably meant /\s+/, or as amphetamachine suggested, /\W+/, depending on how you want to define a “word”.

    That still leaves the problem of the empty “word” you get if the line begins with whitespace. You could split on ' ' to suppress that (it’s a special case), or you could trim leading whitespace first, or insert a grep { length $_ } to weed out empty “words”, or abandon split and use a different method for counting words.

    Processing line by line instead of reading the whole file at once would also be a good improvement, but it’s not as important as those first two items.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.