PDA

View Full Version : Perl Beginner - Stuck! ARGV


welshhuw
03-13-2009, 10:04 PM
Hi everyone,

Im currently learning perl through a home study course and have come a bit stuck writing a program for my course. I have re-read my books over and over and re-written the code but just cannot get it to work.
Im hoping someone can shed some light on this for me!!

I need to check if an argument is passed (as a text file), if not my program needs to prompt the user for a filename.

Then my program needs to check the filename for certain things.

But no-matter what i do, all i get is the die statement printed to screen saying Not Valid. Why?!?!?!?!?


if($#ARGV == -1)
{
print("Enter a FileName Please: ");
$filename = <STDIN>;
chomp($filename);
}
else
{
$filename = $ARGV[0];
}



if ($filename =! m/\w/i)
{
die("Not valid\n");
}

if($filename =! m/\.txt/i)
{
$filename .= ".TXT";
}


Thanks in advance.

Shannon Blonk
03-13-2009, 10:17 PM
You've gotten your operators confused. Try
$filename !~ m/\w/i

FYI:
$filename =! m/\.txt/i gets parsed as
$filename = ( ! ( m/\.txt/i ) )
logical negation of the results of a match test against the default variable $_

welshhuw
03-14-2009, 09:24 AM
I dont think i have my operaters mixed up.
I want it to check to see if the filename does NOT match - If so it will display the die statement.

Do you think i need to 'nest' my statements within one another or something along those lines??

maybe a subroutine ?

Sorry im a total noob!

welshhuw
03-14-2009, 11:26 AM
Anybody!!!??

Help!

FishMonger
03-14-2009, 01:14 PM
Since this is for a course you're taking, I can't just give you the complete answer, but I can guide you.

Why do you think you are using the correct operator?
What do you think are the 2 regex binding operators?

Here are some references that you should read.
http://perldoc.perl.org/perlop.html#Binding-Operators
http://perldoc.perl.org/perlrequick.html
http://perldoc.perl.org/perlretut.html

What are the "certain things" the the filename needs to contain and/or not contain?

welshhuw
03-14-2009, 02:14 PM
The filename should be no more than 8 characters long, first character must not be a digit. The next 7 characters can be digits, letters, underscores etc.
(not including the .txt)


Then if a .txt file extension is not entered, the next if statement must add this to the filename.

I have not used a specific set of reg exp yet as i just wanted to test it would work first.

I appreciate you dont want to give the full answer! I just wwanted to know really which bit/bits i have written wrong??

I have tried entering a print($filename) after the first else clause to see if the variable is correct but i get nothing???

FishMonger
03-14-2009, 03:06 PM
The filename should be no more than 8 characters long
perldoc -f length

first character must not be a digitDoes that mean it could be anything other than a digit? If so, there is a specific character class, similar to \w, that would satisfy that requirement. In most cases, it's more appropriate to test for what is allowed as opposed to what is not allowed. What would be the character class that would satisfy this requirement? The docs I pointed to should help.

The next 7 characters can be digits, letters, underscores etc.
What is included in "etc"? Here you'll need a character class that includes the allowed characters and a quantifier that limits the match to 7 characters.

welshhuw
03-14-2009, 04:24 PM
Ok, i will need to sort out my reg expressions.

Im guessing the first part of my program is all correct then?
Where i check and accept input?

Thanks fishmonger for your help.

I am so frustrated with this!!!

FishMonger
03-14-2009, 04:51 PM
This template might help.
/^[first allowed character][other allowed characters]{quantifier}ext$/

As far as the first part being correct or not is a matter of opinion. If it accomplishes what you need, then it's correct. However, that doesn't mean that it's the best way to do it.

welshhuw
03-14-2009, 05:16 PM
Thanks for your help fishmonger.
I have altered my reg expressions now.

1 strange problem i do have now tho, is the first section of code.
the first if/else clause.
even if the else statement returns true, the print function inside the if clause still processes, but then carries on with the rest of the script below it?!?
its almost there....!