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.
20 Replies - 9320 Views - Last Post: 11 July 2007 - 01:56 PM
#14
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.
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.
#15
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.
#16
Re: Basic Perl Questions/Answers?
Posted 10 July 2007 - 09:50 AM
Apologies for the delay:
The code;
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.
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
#17
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
#18
Re: Basic Perl Questions/Answers?
Posted 11 July 2007 - 04:38 AM
You rule.
Can you explain this lil' bit for me please.
Also, using the split() feature, i thought if i added:
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:
Prints:
Thanks. =]
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
#19
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
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
#20
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 "!".?
If we can use the tr/// to count each character.
Can we use a similar method to count just "?" "." and "!".?
#21
Re: Basic Perl Questions/Answers?
Posted 11 July 2007 - 01:56 PM
yes:
$count =~ tr/?.!/?.!/;
$count =~ tr/?.!/?.!/;

New Topic/Question
Reply



MultiQuote


|