Basic Perl Questions/Answers?

  • (2 Pages)
  • +
  • 1
  • 2

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

#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
  • +
  • -

#16 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 10 July 2007 - 09:50 AM

Apologies for the delay:

The code;

my $file = "input.txt";
my $i = 0;
my $p = 1;

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

while (<READFILE>)
{
$i++;
$p++ if (m/^$/);
}

print ("There are " . $i . " Lines in " . $file . "\n");
print ("There are " . $p . " Paragraphs in " . $file . "\n");

close(READFILE);


Ok so at the moment it counts lines, and paragraphs.

I read the resources i am using, when it comes to regexps it gets confusing.

I know the basics, but when it comes to matching in a file, i can only match a line at a time, when i actually want to match characters and words singly.

This post has been edited by josephman1988: 10 July 2007 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#17 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 11 July 2007 - 01:20 AM

Here is one way to do all those things:



use strict;
use warnings;

my $file = "bad.txt";
my $i = 0;
my $p = 1;
my $words = 0;
my $chars = 0;

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

while (<READFILE>) {
   chomp; [b]# removes the input record seperator[/b]
   $i = $.; [b]# "$." is the input record line number. $i++ will also work[/b]
   $p++ if (m/^$/); [b]# count paragraphs[/b]
   my @t = split(/\s+/); [b]# split sentences into "words" and store them in @t[/b]
   $words += @t; [b]# add count to $words;[/b]
   $chars += tr/ //c; [b]# count all characters except spaces and add to $chars[/b]
}

print "There are $i Lines in $file\n";
print "There are $p Paragraphs in $file\n";
print "There are $words Words in $file\n";
print "There are $chars Characters in $file\n";

close(READFILE);

This post has been edited by KevinADC: 11 July 2007 - 01:45 AM

Was This Post Helpful? 0
  • +
  • -

#18 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 11 July 2007 - 04:38 AM

You rule.

Can you explain this lil' bit for me please.

tr/ //c;


Also, using the split() feature, i thought if i added:

my @sent = (m/\.|\?|\!$/)
$s += @sent;


That this would count the amount of sentances in a file, for e.g:

line goes here,
another goes here,
then 1 also here.

Would print that one sentance was present.

But this file:

Hey, diddle, diddle,
The cat and the fiddle,
The cow jumped over the moon.

The little dog laughed,
To see such sport,
And the dish ran away with the spoon.


Prints:

There are 6 Sentances in input.txt


Thanks. =]

This post has been edited by josephman1988: 11 July 2007 - 08:35 AM

Was This Post Helpful? 0
  • +
  • -

#19 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 11 July 2007 - 10:19 AM

Sentences are hard to count, but you could possibly count them by examining the punctuation. But it may not be accurate unless the grammar is consistent.

tr/ //c

replaces everything in the string with itself, except spaces, and returns the number of such characters replaced. Your perl resource material should cover the 'tr' regexp operator and it's options: c, d, s
Was This Post Helpful? 0
  • +
  • -

#20 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 11 July 2007 - 11:15 AM

I got a good resource for regexps now thanks.

If we can use the tr/// to count each character.

Can we use a similar method to count just "?" "." and "!".?
Was This Post Helpful? 0
  • +
  • -

#21 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 11 July 2007 - 01:56 PM

yes:

$count =~ tr/?.!/?.!/;
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2