在Perl命令行中运行程序后出现空白输出

时间:2015-03-07 04:42:29

标签: arrays perl function

任何人都可以建议,我在Perl cmd中运行命令c:\>perl txt.pl employee_cost.txt master_table.txt以获取以下程序时输出空白:

=============================================== ====

#!/usr/bin/perl

use strict;
use warnings;

#opening of employee_txt:

my $file = $ARGV[0] or die "Need to get txt file on the command line\n";
my $sum = 0;
open(my $employee_cost, '<', $file) or die "Could not open '$file' $!\n";

while (my $line = <$employee_cost>) {
   chomp $line;
   # opening of master_table.txt:
   my $file = $ARGV[0] or die "Need to get txt file on the command line\n";
   open(my $master_table, '<', $file) or die "Could not open '$file' $!\n";
   while (my $line = <$master_table>) {
      chomp $line;
      #declaration of variables:
      my $sal;
      my $name;
      my $attendance;
      my @num_employees =scalar (@_);
      my $rating;
      my @calsal =0;
      my $good;
      my $average;
      my @employees = 0;
      my @array =0;
      my @department= scalar (@_);
      #Finding out which employee is worthy and calculating his salary with 20% bonus and the total num of worthy employees:

      sub cal {
         my ($sal) = @_; # could also write: my $sal = shift @_;
         if ($attendance eq $good and $rating eq $good ^ $average) {
            my $calsal = $sal + 0.2 * $sal;
            my $num_employees = @calsal;
            print "salary = $calsal, the total number of worthy employees = $num_employees\n";
            return @calsal;
         }
         else {
            print "employee is not worthy\n";
            return; # return no value
         }

         #sum of all extra bonus which will be paid to worthy employees: 
         sub bonus {
            my $bonus= scalar (@_);
            my $calsal = 0; 
            foreach $calsal (@_) {
               $bonus += $calsal;
               print "total extra bonus paid by companay is $bonus\n";
            };
         }
      }

      #the department with max num of worthy employees:

      sub department_worthyemployees {
         my $department= scalar (@_);
         my $num_employees = scalar (@_);
         foreach $num_employees(@_) {
            @department = sort { $b <=> $a } @num_employees;
            print "the department name for highest number of worthy employees is $department[0] \n";
         }
      }
   } 
}

=============================================== ====

'employee_cost.txt'文件的内容:


name, department, attendence, rating, sal:

A, HR, bad, bad, 1000

B, Sales, good, good, 3000

C, Admin,bad, average, 1500

D,Admin, bad,average, 2000

E,Contract temp, good, average, 2000

F, production, good, bad, 1500

'master_table.txt'的内容:


department number,department,hike(%),number of employees:

1,HR,10%,30

2,marketing,10%,100

3,sales,5%,50

4,admin,15%,20

5,production,14%,60

6,contract_temporary,15%,60

1 个答案:

答案 0 :(得分:1)

这里有很多问题:

  1. 您永远不会打开master_table.txt。而是打开employee.txt两次。
  2. 子程序永远不会被调用,所以它们永远不会打印任何东西。
  3. 您混淆数组(以@开头)和标量变量(以$开头)。
  4. 特殊变量@_包含子例程中的子例程参数。它不包含该背景之外的任何内容。
  5. 我认为这里的问题是你在检查它是否正常工作之前编写了太多代码。您需要做的是编写一小步的代码,并在继续下一步之前验证它是否有效。

    例如,编写打开员工文件并打印每一行的代码。一旦看到这种方法有效,请转到下一步(打开另一个文件并打印内循环中的每一行)。学习如何使用Perl debugger也将有助于此过程。