Web Design and Development Forums

Understadning Perl Basics

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

Old Jul 7th, 2007, 11:15   #1 (permalink)
Junior Member
 
Join Date: Dec 2006
Location: London
Age: 19
Posts: 32
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.
josephman1988 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Aug 9th, 2007, 17:53   #2 (permalink)
Junior Member
 
Join Date: Aug 2007
Location: chesterfield
Age: 36
Posts: 24
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.
DPhahn is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Aug 9th, 2007, 18:14   #3 (permalink)
Junior Member
 
Join Date: Aug 2007
Location: chesterfield
Age: 36
Posts: 24
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.
DPhahn is offline  
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
Rate This Thread
Rate This Thread:

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 HTML Forum 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



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 23:02.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59