Nathan_84 has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks :) I've been working my through a number of tutorial as I try to increase my wisdom in perl. However the one scripts I have come across does not simply start! Everytime it's executed it returns to command line. </p. BTW this is the contents of the employees.txt file

Mr John Doe R&D 21000 Miss Gloria Dunne HR 23000 Mr Jack Stevens HR 45000 Mrs Julie Fay R&D 30000 Mr Patrick Reed R&D 33000
Any help or advice would be greatly welcomed. Thanks! ------------------------------------------------------------------------- Hi all just thought I'd ley you know that I think I got the script to do the following: The purpose of this is to perform the following:
1. Identifies the name, department and salary of the employee
2. Ignores and goes to the next line if the employee is female (the name does not start with Mr)
3. Ignores and goes to the next line if the salary is less or equal to 25000.
4. Ignores and goes to the next line if the department is not "R&D".
5. Prints the name and the salary of the employee on the screen.
By using the following code
#!/usr/bin/perl #open the employeesfile open (EMPLOYEES,"employees.txt") or die "Tough: $!\n\n"; #for each line while ($line = <EMPLOYEES>) { #remove the carriage return chomp $line; #split the line between tabs #and get the different elements ($name,$department, $salary) = split /\t/, $line; #go to the next line unless the name starts with "Mr " next unless$name =~ /^Mr /; #go to the next line unless the salary is more than 25000. next unless$salary > 5000; #go to the next line unless the department is R&D. next unless$department eq $ARGV[0]; #since all employees here are male, #remove the particle in front of their name $name =~ s/Mr//; print"$name\n"; print"$department\n"; print"$salary\n"; } close (EMPLOYEES);
I then entered the following at the command screen: ./filename.pl "R&D" which produced this:
John Doe R&D 21000 Patrick Reed R&D 33000 Thanks for your help and advice!! :)

Replies are listed 'Best First'.
Re: Reading from filehandles in a loop and matching lines
by almut (Canon) on May 06, 2010 at 00:27 UTC
    ...does not simply start! Everytime it's executed it returns to command line.

    Maybe it runs, but doesn't print anything, e.g. because the department ('R&D' as per comment in the code) is not passed in $ARGV[0], or passed incorrectly (at least on Unix, the argument would need to be quoted because of the & )?

    Are you running it like this

    $ ./838601.pl 'R&D'

    When I do so, it prints " Patrick Reed".

    (assuming you actually have tabs in your input file — which is hard to tell, as cut-n-pasting might have changed them to spaces...).

      Hi, You were spot on with the tabs error! I'm so blond! Can't believe I didn't see that. Thanks.

      You the purpose of this script was so that it can do the following:
      1. Identifies the name, department and salary of the employee
      2. Ignores and goes to the next line if the employee is female (the name does not start with Mr)
      3. Ignores and goes to the next line if the salary is less or equal to 25000.
      4. Ignores and goes to the next line if the department is not "R&D".
      5. Prints the name and the salary of the employee on the screen.
Re: Reading from filehandles in a loop and matching lines
by chromatic (Archbishop) on May 06, 2010 at 00:26 UTC

    This comment is a lie:

    #go to the next line unless the department is R&D. next unless$department eq $ARGV[0];

    Unless you invoke this program with an argument of either R&D or HR, you'll see no output.

      So too is line 25:
      #since all employees here are male,
      ...unless, of course, "Mrs Julie Fay" is male.

        You missed this:

        #go to the next line unless the name starts with "Mr " next unless$name =~ /^Mr /;

Re: Reading from filehandles in a loop and matching lines
by toolic (Bishop) on May 06, 2010 at 00:29 UTC
    Add use warnings; to the top of your code, then eliminate the warning messages you get (see use strict and warnings).

    I wonder if your input file really has spaces instead of tab characters. Add these lines after your split line for debugging purposes:

    print "n=$name\n"; print "d=$department\n"; print "s=$salary\n";

    It would have helped us if you showed the command line you used to run your code. For example, I tried this, and it seemed to work:

    $ 838601.pl HR Jack Stevens

    See also: How do I compose an effective node title?

Re: Reading from filehandles in a loop and matching lines
by choroba (Cardinal) on May 06, 2010 at 00:32 UTC
    When testing the department, you compare $department to $ARGV[0]. Have you started your script with a parameter (HR or "R&D")?
      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.