Understadning Perl Basics

This is a discussion on "Understadning Perl Basics" within the Other Programming Languages section. This forum, and the thread "Understadning Perl Basics are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Other Programming Languages

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jul 7th, 2007, 11:15
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Aug 9th, 2007, 17:53
Junior Member
Join Date: Aug 2007
Location: chesterfield
Age: 37
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Understadning Perl Basics

try this
Code: Select all
my $lin = "Lines: \n";
chomp($lin);
enter:
{
print("Please enter a file name: ");
$fileName = <STDIN>;
chomp($fileName);
 
if($fileName !~ m/\.TXT$/i)
 {
  open(READFILE, "$fileName.txt") || die "couldn't open file: $!";
        }
else
 {
 print("re-enter filename!\n");
 redo enter;
}
}
while(<READFILE>)
{
package Lines;
 $count += split(/\n\n/, $_);
}
print("$lin $Lines::count\n");
close(READFILE);

Last edited by karinne; Aug 9th, 2007 at 17:54. Reason: Please use [ code ]...[ /code ] tags when displaying code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Aug 9th, 2007, 18:14
Junior Member
Join Date: Aug 2007
Location: chesterfield
Age: 37
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Understadning Perl Basics

OR this..yu missed the backslash before .txt...if ($file !~ m/.txt$/)
it should be if ($file !~ m/\.txt$/)
Code: Select all
my ($lastchar,$lines,$file,$paragraphs,$ch);
print("enter filename: ");
my $file = <STDIN>;
chomp($file);
if ($file !~ m/\.txt$/)
{
$file .= ".txt";
} 
open(READFILE, "$file") || die "No File Found";
my $i = 1;
while ($ch = getc(READFILE))
{
if ($ch eq "\n") # if newline...
 {
  $lines++; # count another line
  if ($lastchar eq "\n") # if last character also newline
  {
  $paragraphs++; # count another paragraph
  }
 }
}
print ("There are " . $i . " Lines in " . $file);
close(READFILE);

Last edited by karinne; Aug 9th, 2007 at 18:16. Reason: Again ... please use [ code ]...[ /code ] tags when displaying code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
basic, perl

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
mod rewrite basics shared hosting maniac PHP Forum 6 Feb 28th, 2008 23:09
Getting the basics right... nuddy Web Page Design 11 Jan 13th, 2007 20:47
Can somebody lay down the basics of PHP for me plz Physt PHP Forum 2 Oct 10th, 2004 13:32


All times are GMT. The time now is 11:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42