in reply to Reading from filehandles in a loop and matching lines

When testing the department, you compare $department to $ARGV[0]. Have you started your script with a parameter (HR or "R&D")?

Replies are listed 'Best First'.
Re^2: Reading from filehandles in a loop and matching lines
by Nathan_84 (Acolyte) on May 06, 2010 at 01:37 UTC
    use warnings; use strict; open (EMPLOYEES,"employees.txt") or die "Tough: $!\n\n"; while ($line = <EMPLOYEES>) { chomp $line; ($name,$department, $salary) = split /\t/, $line; print "n=$name\n" print "d=$department\n" print "s=$salary\n" next unless$name =~ /^Mr /; next unless$salary > 25000; next unless$department eq $ARGV[0]; $name =~ s/Mr//; print"$name\n"; } close (EMPLOYEES);

    The command line i used was ./employee HR. This is the whole script and there are no parameters at the start. These are the errors i am getting;

    ./employee.pl: line 1: use: command not found ./employee.pl: line 2: use: command not found ./employee.pl: line 5: syntax error near unexpected token `EMPLOYEES," +employees.txt"' ./employee.pl: line 5: `open (EMPLOYEES,"employees.txt") or die "Tough +: $!\n\n";'
      Looks like shell error messages. Have you deleted the very first line of the script starting with #!? Then your shell tries to interpret the script, not perl.

        with the first line added i get the following errors

        l symbol "$line" requires explicit package name at ./employee.pl line +10. Global symbol "$line" requires explicit package name at ./employee.pl +line 13. Global symbol "$name" requires explicit package name at ./employee.pl +line 16. Global symbol "$department" requires explicit package name at ./employ +ee.pl line 16. Global symbol "$salary" requires explicit package name at ./employee.p +l line 16. Global symbol "$line" requires explicit package name at ./employee.pl +line 16. Global symbol "$name" requires explicit package name at ./employee.pl +line 18. syntax error at ./employee.pl line 19, near "print" Global symbol "$name" requires explicit package name at ./employee.pl +line 23. Global symbol "$salary" requires explicit package name at ./employee.p +l line 26. Global symbol "$department" requires explicit package name at ./employ +ee.pl line 29. Global symbol "$name" requires explicit package name at ./employee.pl +line 32. Global symbol "$name" requires explicit package name at ./employee.pl +line 34. Execution of ./employee.pl aborted due to compilation errors.