View Single Post
  #1 (permalink)  
Old Jul 7th, 2007, 11:15
josephman1988 josephman1988 is offline
Junior Member
Join Date: Dec 2006
Location: London
Age: 20
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Understadning Perl Basics

Hi,

The reason for the topic tilte is because I'l be asking various questions in the same thread over a period of time, easier then making a million threads!

Anyway, I'm having a problem with one of my scripts.

What I want to do is read a file that the user inputs.
E.g - stats.txt will open the file.

However, what i want to do, is that if the user doesn't enter a extension of .txt it will automatically add it.

My attempt:

Code: Select all
my $file = <STDIN>;
open(READFILE, "$file") || die "No File Found";

if ($file !~ m/.txt$/)
{
$file .= ".txt";
} else {
print "There was already a .txt filename\n";
}

my $i = 0;
while (<READFILE>)
{
$i++;
}
print ("There are " . $i . " Lines in " . $file);
close(READFILE);
However, this doesn't work. =[

Any help is greatly appreciated.
Reply With Quote