Re: Perl Project: Text Search
by moritz (Cardinal) on Apr 08, 2010 at 11:09 UTC
|
. I went on to the server and found the password.bak file mentioned, but I don't think we are allowed to access it.
Don't assume things -- try it out. You will get an error message for opening a file you don't have access to.
I'm kind of curious how I'm supposed to interact with this file in my program if I can't even view its contents.
If you really can't read it, you can't. Then approach your tutor and ask him what's wrong. But try first.
If anybody could direct me to some relevant tutorials or give me any kind of help here at all I'd greatly appreciate it
perlintro, http://learn.perl.org/, Tutorials
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] |
|
I did try to access it. I tried opening it and it said "you do not have access to this file"
I thought it was a little silly too. I just wanted to make sure there wasnt some obvious way of accessing the file that im not aware of, before asking my teacher. thanks.
| [reply] |
Re: Perl Project: Text Search
by marto (Cardinal) on Apr 08, 2010 at 11:14 UTC
|
"I don't think we are allowed to access it."
Umm, have you actually tried to access it? If the homework you've been given depends on accessing a file I suspect the powers that be will grant you access to do so.
"If anybody could direct me to some relevant tutorials or give me any kind of help here at all I'd greatly appreciate it. Thanks."
You've been tasked with writing a perl script, have you been to any classes? The problem you've been asked to solve is fairly typical for a first programming task. Perhaps working through some of Getting Started with Perl from the tutorials section of this site would be a good place to start, along with reading http://learn.perl.org.
| [reply] |
|
I did try to access it. It says "you do not have access to this file". How do I go about opening a .bak file anyway?
It's an online class, I've completed 10 programming assignments already, this is our second perl assignment. I just wanted to make sure I wasnt missing something obvious before asking my teacher why I can't access the file. Thanks for your feedback.
| [reply] |
|
I suspect the passwd.bak in question is simply a copy of a *nix passwd, the .bak extension suggests that it's an backup copy rather than the live passwd file. You'll simply read the file as though it were any other text file. If you can't open the file for reading then you may need to contact the person who assigned the task. Good luck.
| [reply] |
|
|
|
How do I go about opening a .bak file anyway?
open and see perlopentut. The fact that the filename ends in .bak is irrelevant - what is important is the format of the data within.
| [reply] |
|
Re: Perl Project: Text Search
by cdarke (Prior) on Apr 08, 2010 at 12:21 UTC
|
if anybody could give me a hint as to how to get started on this, I'd be very much in your debt
Read your course notes! Ok, these are the steps: open the file Use a while loop to read a line from the file handle Within the loop, split the line around the field delimiter, extracting the relevant columns. Test the data found against that required. If found, leave the loop. Outside the loop, close the file handle print the result. | [reply] |
|
You sir are a scholar and a gentleman, thank you very much. I will come back and post my code when I get something up.
| [reply] |
Re: Perl Project: Text Search
by MidLifeXis (Monsignor) on Apr 08, 2010 at 14:04 UTC
|
A + vote for being up front on the fact that you are a student, this is an assignment, etc. It looks like other monks have pointed you in the right direction, but if you run into any stumbling blocks, come on back.
My wife is walking for a cure for MS. Please consider supporting her.
| [reply] |
|
I think I got it all figured out now, I just am having trouble with the error trapping. My error trap for a nonexistent username doesnt work.
#!/usr/bin/perl
$filename = '/etc/passwd.bak';
open(FILE, $filename) or die "Could not read from $filename, program h
+alting.";
while(<FILE>)
{
chomp;
@fields = split(':', $_);
if ($ARGV[0] eq $fields[0]){
print "Home directory: $fields[5]\n";
print "Shell used: $fields[6]\n";
exit 0;
} elsif ($ARGV[0] != $fields[0]){
print "User: username does not exist.\n";
exit 0;
} elsif ((@ARGV > 1) || (@ARGV == 0)){
print "Please enter at least one but only one argument.\n";
exit 0;
}
}
close FILE;
| [reply] [d/l] |
|
Your two elsif blocks don't belong in the loop. If they're in the loop, they execute for every line of the file, which is not what you want.
You only know for sure that a username wasn't in the file after you've worked through every single line of the file. In fact, since you immediately exit the program after finding and printing the information for a user, you know that if you make it through the entire while(<FILE>) loop, the username wasn't found. Print the error message at that point.
Similarly, the check that exactly one argument was provided needs to be done before you enter the while loop, not within it. Why even open the passwd file if the program wasn't run correctly, right?
Update: the indentation of your code is strange - you do realize that the if/elsif/elsif is within the while loop - hence is being executed once for every line in the input file? Nesting in Perl is controlled with curly braces ({ and }), not by indentation like in some other languages.
| [reply] [d/l] [select] |
|
|
|
That was me that posted the code just forgot to login.
| [reply] |