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 785385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:52:10+00:00 2026-05-14T20:52:10+00:00

I have basically the following perl I’m working with: open I,$coupon_file or die Error:

  • 0

I have basically the following perl I’m working with:

open I,$coupon_file or die "Error: File $coupon_file will not Open: $! \n";
while (<I>) {
 $lctr++;
 chomp;
 my @line = split/,/;
 if (!@line) {
     print E "Error: $coupon_file is empty!\n\n";
     $processFile = 0; last;
 }
}

I’m having trouble determining what the split/,/ function is returning if an empty file is given to it. The code block if (!@line) is never being executed. If I change that to be

if (@line)

than the code block is executed. I’ve read information on the perl split function over at
http://perldoc.perl.org/functions/split.html and the discussion here about testing for an empty array but not sure what is going on here.

I am new to Perl so am probably missing something straightforward here.

  • 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-14T20:52:10+00:00Added an answer on May 14, 2026 at 8:52 pm

    Below are a few tips to make your code more idiomatic:

    • The special variable $. already holds the current line number, so you can likely get rid of $lctr.
    • Are empty lines really errors, or can you ignore them?
    • Pull apart the list returned from split and give the pieces names.
    • Let Perl do the opening with the "diamond operator":

    The null filehandle <> is special: it can be used to emulate the behavior of sed and awk. Input from <> comes either from standard input, or from each file listed on the command line. Here’s how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loop

    while (<>) {
    ... # code for each line
    }
    

    is equivalent to the following Perl-like pseudo code:

    unshift(@ARGV, '-') unless @ARGV;
    while ($ARGV = shift) {
      open(ARGV, $ARGV);
      while (<ARGV>) {
      ... # code for each line
      }
    }
    

    except that it isn’t so cumbersome to say, and will actually work.

    Say your input is in a file named input and contains

    Campbell's soup,0.50
    Mac & Cheese,0.25

    Then with

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    die "Usage: $0 coupon-file\n" unless @ARGV == 1;
    
    while (<>) {
      chomp;
    
      my($product,$discount) = split /,/;
      next unless defined $product && defined $discount;
    
      print "$product => $discount\n";
    }
    

    that we run as below on Unix:

    $ ./coupons input
    Campbell's soup => 0.50
    Mac & Cheese => 0.25
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, basically i have an empty AS3 fla file with just the following
I have a text file (basically an error log with date, timestamp and some
I basically have the following string in the format: A,B,C:D,E,F What I am trying
I basically have the following class: .sf-sub-indicator { background: url(/abcprod/images/arrows-ffffff.png) no-repeat -10px -100px; }
I am wondering if the following is possible in MVC 3 I basically have
Basically I have a new client that is after the following functionality from her
Is there any name for the following DB table design: Basically we have generic
I have created following thing in android using android compatibility support package Basically i
I have the following views in my application. Basically I want to call show_house()
Basically, I have the following so far: class Foo { public override bool Equals(object

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.