Basic Perl Questions/Answers?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 9069 Views - Last Post: 11 July 2007 - 01:56 PM

#1 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Basic Perl Questions/Answers?

Post icon  Posted 07 July 2007 - 10:53 AM

Hi,

The reason for the topic title 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:

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Basic Perl Questions/Answers?

#2 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 11:16 AM

Saying "it doesn't work" is not very helpful. Post any error messages or warnings the script outputs or describe what the script does do.

In your case I see one problem right away:

my $file = <STDIN>;


$file will end with an input terminator, probably a newline, so it will not be able to open a file named "file.txt\n" which your die statment should have caught and echoed back an error. You need to use chomp() on your input:

chomp(my $file = <STDIN>);


from now on, start all your perl script with these two pragmas:

use strict;
use warnings;


and you can add this one for good measure:

use diagnostics;


The first two are very important, especially for new perl coders. The third one dplays very verbose messages and is sometimes more useful than the brief messages that perl outputs by default.

If your perl resource material did not mention the need to chomp() input, I would stop using it.

Besides all that, your script looks like it should work once the chomp() is added.

Kevin
Was This Post Helpful? 0
  • +
  • -

#3 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 11:40 AM

Apologies.

Whether we have the:

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


Or not, we can still bring up the .txt document by typing in the extension.

After adding the warnings, strict and diagnostics, and then running the script again.Without supplying the extension i am faced with the following error.
(BTW, I am using the command line);

Posted Image

With the code being ultimately altered to:

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

chomp(my $file = <STDIN>);
open(READFILE, "$file") || die "Dead";

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 . "\n");
close(READFILE);

Was This Post Helpful? 0
  • +
  • -

#4 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 11:51 AM

change this line:

open(READFILE, "$file") || die "Dead";


to:

open(READFILE, "$file") or die "Can't open file '$file': $!";


$! stores the operating system error message, which you will need to see why the file is not being opened by the perl script. Where are you learning perl from? This is quite useless:

die "Dead";

I hope your perl resource material did not suggest using that useless construct. You should also use the lower precedence "or" for program flow control instead of "||".
Was This Post Helpful? 0
  • +
  • -

#5 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 12:07 PM

I used the:

die "Dead";

As i didn't think it mattered what i actually printed at this time?
Oh so i understand why the $! is now used, thanks.

The error message is now:

stats
Can't open file 'stats': No such file or directory at
		C:\DOCUME~1\J-Man\LOCALS~1\Temp\dir798.tmp\RF.pl line 7, <STDIN> line 1
(#1)
	(S inplace) The implicit opening of a file through use of the <>
	filehandle, either implicitly under the -n or -p command-line
	switches, or explicitly, failed for the indicated reason.  Usually this
	is because you don't have read permission for a file which you named on
	the command line.

Uncaught exception from user code:
		Can't open file 'stats': No such file or directory at C:\DOCUME~1\J-Man\
LOCALS~1\Temp\dir798.tmp\RF.pl line 7, <STDIN> line 1.
 at C:\DOCUME~1\J-Man\LOCALS~1\Temp\dir798.tmp\RF.pl line 7

This post has been edited by josephman1988: 07 July 2007 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 12:26 PM

The problem is that file "stat" can't be found. I would think you would want to check/add the "txt" extension before trying to open the file. Also, the file has to be in the same directory as the script if you don't use a path to the file when you try and open the file.

I'll check back later, I've got stuff to do.
Was This Post Helpful? 0
  • +
  • -

#7 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 12:30 PM

Omg, trust it to be a simple snippet of code being added in the wrong place.

Thanks for the help mate.

Appreciate your time. =]
Was This Post Helpful? 0
  • +
  • -

#8 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 02:11 PM

If i wanted to put in another feature, where the user has to input a filename with only letters, would i have to put this in the same code block or a separate on?

For E.g -
until ($file =~ m/[a-zA-Z])

How would i repeat this process until the user inputs a valid set of characters?
Would i use a 'redo LOOP:' method or what?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Basic Perl Questions/Answers?

Posted 07 July 2007 - 11:07 PM

View Postjosephman1988, on 7 Jul, 2007 - 02:11 PM, said:

If i wanted to put in another feature, where the user has to input a filename with only letters, would i have to put this in the same code block or a separate on?

For E.g -
until ($file =~ m/[a-zA-Z])

How would i repeat this process until the user inputs a valid set of characters?
Would i use a 'redo LOOP:' method or what?

Thanks.


Well, there are a number of ways to do something like this. You sort of have the correct concept. The regexp is not correct though because it is only checking to see if there is at least one of those characters any where in the string. To check the entire string you have to use string anchors:

^ (beginning of string)
$ (end of string)

until ($file =~ m/^[a-zA-Z]{10,20}$)


the {10,20} checks for a min and max length.
Was This Post Helpful? 0
  • +
  • -

#10 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Re: Basic Perl Questions/Answers?

Posted 08 July 2007 - 05:50 AM

That worked like a charm. =]

Last question on this matter I promise!

Say the text file being read has 2 paragraphs, being separated by 2 newlines.

How would I count the paragraphs in the text file and print the results?

:^:
Was This Post Helpful? 0
  • +
  • -

#11 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Basic Perl Questions/Answers?

Posted 08 July 2007 - 02:59 PM

One way is to change the input record seperator to "\n\n"

use strict;
use warnings;

{
   local $/ = "\n\n";
   my @paragraphs  = <DATA>;
   print scalar @paragraphs;
}



__DATA__
this is the first paragraph
this is the first paragraph
this is the first paragraph
this is the first paragraph

this is the second paragraph
this is the second paragraph
this is the second paragraph
this is the second paragraph

Was This Post Helpful? 0
  • +
  • -

#12 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Re: Basic Perl Questions/Answers?

Posted 09 July 2007 - 02:41 AM

I managed to find a simple way.
$p++ if (m/^$/);


But now I'm really confused?
I want to count characters in the file, whitespace and a-z only.
As well as words and sentences.

So i tried the following for words

$c++ if (m/^\w+\s+/);
- This actually counts the lines excluding paragraphs -

Don't know how to count single characters and words though?
Was This Post Helpful? 0
  • +
  • -

#13 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Basic Perl Questions/Answers?

Posted 09 July 2007 - 10:31 AM

Is this all school/course work?

Your method for counting paragraphs can work, but unless there are two newlines at the very end it will count one less paragraph than there actually is, but then you can just add one to the count to make up for that.
Was This Post Helpful? 0
  • +
  • -

#14 josephman1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 07

Re: Basic Perl Questions/Answers?

Posted 09 July 2007 - 11:21 AM

No it's not.

I'm learning prel, and im in the reading a file phase.
And this is just teaching me now how to match data in a file.
Yea i understood that lol, reason i changed my $p = 1;

I know now that this is how we read lines, but i don't know the way to asses each character, could i also use this same method of:

$w++ if(m//);

Thanks again for all this help.
Was This Post Helpful? 0
  • +
  • -

#15 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Basic Perl Questions/Answers?

Posted 09 July 2007 - 11:52 AM

It seems you are jumping ahead of yourself or your perl resource did not cover regexp's very well. Post your current code and I will check back later today.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2