CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need some help :)

 



morrison1976
Novice

Nov 11, 2008, 11:25 AM

Post #1 of 16 (1547 views)
Need some help :) Can't Post

I have just started doing a course, and at the moment im learning perl. I am stuck and would be grateful if anyone can help me.
I have made a folder called perl scripts, and thats where i put my perl files. Also, i have a txt document call files, which is filled with names and numbers, and this us where im having the problem.

I have made this perl script called lab4-2.1.pl

#!/usr/bin/perl

while(<STDIN>)
{
if (m/([^\t]*)\t/)
{
@employeeNames[$#employeeNames + 1] = $1;
}
}

for ($i = 0; $i <= $#employeeNames; $i++)
{
print("$employeeNames[$i]\n");
}

I have been told to open the command prompt and get the information from the employee text document.
So i open up the command prompt, locate the folder and wrote this

lab4-1.2.pl < employee.txt and nothing happens. Its doing my head in. I have done what it said in my course. Can someone please help me :)


vassago
Novice

Nov 14, 2008, 4:02 PM

Post #2 of 16 (1530 views)
Re: [morrison1976] Need some help :) [In reply to] Can't Post

are you getting errors? what's your output?

i don't understand your use of '#'
your !/usr/bin/perl is commented out, so you don't need that.

i really don't understand what your script is supposed to do.

if you add use warnings; and use strict; if gives some useful errors. i'd start there.


FishMonger
Veteran / Moderator

Nov 14, 2008, 4:29 PM

Post #3 of 16 (1529 views)
Re: [vassago] Need some help :) [In reply to] Can't Post


In Reply To
i don't understand your use of '#'
your !/usr/bin/perl is commented out, so you don't need that.

That's the shebang line and the '#' is a required portion of its syntax.

http://en.wikipedia.org/wiki/Shebang_(Unix)

I do agree that the purpose of the script is elusive.


morrison1976
Novice

Nov 15, 2008, 1:24 AM

Post #4 of 16 (1525 views)
Re: [FishMonger] Need some help :) [In reply to] Can't Post

I will write what it says in my course. It says write the following code

#!c:/usr/bin/perl.exe


while(<STDIN>)
{
if (m/([^\t]*)\t/)
{
@employeeNames[$#employeeNames + 1] = $1;
}
}

for ($i = 0; $i <= $#employeeNames; $i++)
{
print("$employeeNames[$i]\n");
}

save the lab as lab4-1.2.pl

Prompt- Execute the script using the employee,txt file from lab 3-3 as the redirected input

lab4-1.2.pl < employee.txt ( this txt file is filled with names and phone numbers)

Modify the script so that it produces an alphabetical listing of employees.


(This post was edited by morrison1976 on Nov 15, 2008, 1:26 AM)


KevinR
Veteran


Nov 15, 2008, 5:20 AM

Post #5 of 16 (1517 views)
Re: [morrison1976] Need some help :) [In reply to] Can't Post


Quote
Modify the script so that it produces an alphabetical listing of employees.


What have you tried?
-------------------------------------------------


morrison1976
Novice

Nov 15, 2008, 5:30 AM

Post #6 of 16 (1516 views)
Re: [KevinR] Need some help :) [In reply to] Can't Post

I have not tried modifying the script yet, as im trying to get it to work. My other scripts work that i have done so far through command prompt, but the lessons that involve the employee.txt file i just cant get to work. Im doing what it says in the course. What i think should happen is when i go into command prompt and type lab4-1.2.pl < employee.txt then it should bring up information, but it does not.

c:\perlscripts>lab4-1.2.pl <employee.txt

I press enter, then it goes back to this

c:\perlscripts>

And thats it. Its doing my head in!


KevinR
Veteran


Nov 15, 2008, 5:50 AM

Post #7 of 16 (1514 views)
Re: [morrison1976] Need some help :) [In reply to] Can't Post

OK, just do this for right now:


Code
#!c:/usr/bin/perl.exe  

while(<STDIN>)
{
print;
}


That will hopefully print the lines of the file to the screen. If it does, then the problem is the 'if' condition in your script is never true so no data is ever appended to the array and thus there is nothing to display.

If the above still displays nothing make sure the file is in the same directory as the script or use the full path to the file instead of just the filename.

Maybe your computer is setup such that you don't have to start perl when you run a .pl file, but normally you run a perl script like this (assumes perl is in the command path):

perl nameofscript < filename
-------------------------------------------------


FishMonger
Veteran / Moderator

Nov 15, 2008, 10:44 AM

Post #8 of 16 (1502 views)
Re: [KevinR] Need some help :) [In reply to] Can't Post


In Reply To
... normally you run a perl script like this (assumes perl is in the command path):

perl nameofscript < filename


Sorry Kevin, but that's not the normal way to execute a perl script, but it is one of the methods that can be used.

The normal and preferred way is simply:

Code
nameofscript.pl < filename



FishMonger
Veteran / Moderator

Nov 15, 2008, 10:55 AM

Post #9 of 16 (1500 views)
Re: [morrison1976] Need some help :) [In reply to] Can't Post

Are really saying that your instructor provided that code? If that's true, then my recommendation would be drop the class and find another instructor. That's the type of code I'd expect from a new student, not from an instructor that is trying to teach good Perl programming practices.

Since you haven't shown us the contents of employee.txt, we can't say exactly what needs to be done to fix the script. If you post its contents, we can give a you couple suggestions, but since this is your homework assignment, we won't provide a complete solution.


KevinR
Veteran


Nov 15, 2008, 11:01 AM

Post #10 of 16 (1498 views)
Re: [FishMonger] Need some help :) [In reply to] Can't Post


In Reply To

In Reply To
... normally you run a perl script like this (assumes perl is in the command path):

perl nameofscript < filename


Sorry Kevin, but that's not the normal way to execute a perl script, but it is one of the methods that can be used.

The normal and preferred way is simply:

Code
nameofscript.pl < filename



As far as I know that is the "normal" way on Windows.
-------------------------------------------------


FishMonger
Veteran / Moderator

Nov 15, 2008, 11:11 AM

Post #11 of 16 (1496 views)
Re: [KevinR] Need some help :) [In reply to] Can't Post


In Reply To
As far as I know that is the "normal" way on Windows.


Nope, not true.

If Perl was installed correctly, a file type association would have been created that points files with the .pl extension to be executed by perl. There are exceptions depending on which Perl port was installed, but most of the main ports (Activestate being the most common) create the file association and if they don't it's recommended to have the user manually add the association.


KevinR
Veteran


Nov 15, 2008, 12:41 PM

Post #12 of 16 (1494 views)
Re: [FishMonger] Need some help :) [In reply to] Can't Post

Yea, thats true. I forgot that I changed mine manually to open in my IDE instead of have perl run the file. But if a person does the default installation then their .pl files should be associated with the perl executable and run as you say.
-------------------------------------------------


morrison1976
Novice

Nov 16, 2008, 4:40 AM

Post #13 of 16 (1473 views)
Re: [KevinR] Need some help :) [In reply to] Can't Post

Here is the txt file content for employee.txt



Jane Doe Personnel 714.555.1234 x100
John Doe Finance 714.555.1234 x200
Ed Harris Education 510.555.4321 x300
Fred Fish Sales 714.555.6677 x400
Sam Jones Personnel 818.555.3995 x101
Pam Smith Finance 310.555.1210 x201
Bill Reid Education 714.555.6334 x301
Elliot Smith Personnel 818.555.1434 x134
Frank Hamner Finance 714.555.1256 x234
Jim Fields Education 510.555.4888 x334
Tricia Mays Sales 714.555.6707 x440
Jane Pauley Personnel 818.555.3005 x171
Pam Hanks Finance 310.555.1667 x281
Hank Jones Education 714.555.8834 x355
Jane Mintz Personnel 714.555.5532 x190
Ken McGraw Finance 714.555.1004 x270
Ellen Ingersoll Education 510.555.9911 x391
Tim Duncan Sales 714.555.6011 x430
Sid Barret Personnel 818.555.3275 x143
Polly Bergen Finance 310.555.5521 x271
Allen Taylor Education 714.555.4008 x399
Jill Hanson Personnel 714.555.6690 x155
George Harrison Finance 714.555.1001 x299
Edward Bellows Education 510.555.9921 x379
Ed Ames Sales 714.555.9911 x479
Sal Bundy Personnel 818.555.0215 x172
Paula Brown Finance 310.555.1888 x263
Nathan Wright Education 714.555.8899 x333


FishMonger
Veteran / Moderator

Nov 16, 2008, 9:03 AM

Post #14 of 16 (1464 views)
Re: [morrison1976] Need some help :) [In reply to] Can't Post

The problem you're experiencing is do to a difference between Windows and *nix on how the command is seen/parsed/executed when input redirection is used.

Here's my output from running it on Windows.

Quote

C:\test>lab4-1.2.pl < employee.txt

C:\test>perl lab4-1.2.pl < employee.txt
Jane Doe
John Doe
Ed Harris
Fred Fish
Sam Jones
Pam Smith
Bill Reid
Elliot Smith
Frank Hamner
Jim Fields
Tricia Mays
Jane Pauley
Pam Hanks
Hank Jones
Jane Mintz
Ken McGraw
Ellen Ingersoll
Tim Duncan
Sid Barret
Polly Bergen
Allen Taylor
Jill Hanson
George Harrison
Edward Bellows
Ed Ames
Sal Bundy
Paula Brown
Nathan Wright


Here's the exact same script run on Fedora

Quote

[root@fc4dev ~]# ./lab4-1.2.pl < employee.txt
Jane Doe
John Doe
Ed Harris
Fred Fish
Sam Jones
Pam Smith
Bill Reid
Elliot Smith
Frank Hamner
Jim Fields
Tricia Mays
Jane Pauley
Pam Hanks
Hank Jones
Jane Mintz
Ken McGraw
Ellen Ingersoll
Tim Duncan
Sid Barret
Polly Bergen
Allen Taylor
Jill Hanson
George Harrison
Edward Bellows
Ed Ames
Sal Bundy
Paula Brown
Nathan Wright



morrison1976
Novice

Nov 16, 2008, 11:08 AM

Post #15 of 16 (1462 views)
Re: [morrison1976] Need some help :) [In reply to] Can't Post

What should i do. No matter what i try, i cant get it too work :(:(:(:(::(


KevinR
Veteran


Nov 17, 2008, 3:15 PM

Post #16 of 16 (1443 views)
Re: [morrison1976] Need some help :) [In reply to] Can't Post

Did you try the suggestions in my post above?
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives