Re: Perl Help
by marto (Cardinal) on Jan 22, 2008 at 11:14 UTC
|
| [reply] |
Re: Perl Help
by svenXY (Deacon) on Jan 22, 2008 at 11:10 UTC
|
Hi,
glad you made it back here!
Almost everybody is probably willing to even help you with your coursework (a.k.a homework) if you first show us what you have tried yourself. Few will probably be eager to wrtite your coursework for you though.
You get the point? Good! No? Read How (Not) To Ask A Question (especially "Do your own work") and then ask your question.
Looking forward to your post,
svenXY
PS: I'm not a hardcore perl programmer and many of us aren't either PPS: Most of us have started they way you (hopefully) do now! | [reply] |
|
|
I'm trying to count the instances of certain characters within a text file.
Here is what I have written so far. I'm having a problem with the brackets. Its picking them up as character classes. Help.
open (READFILE, "<readme.txt") || die "Couldn't open file: $!";
$buffer = "";
while(<READFILE>) {
$commas++ while ($_ =~ m/,/g);
$fullstops++ while ($_ =~ m/\./g);
$openbracket++ while ($_ =~ m/[/g);
$closedbracket++ while ($_ =~ m/]/g);
$hypehns++ while ($_ =~ m/-/g);
}
| [reply] [d/l] |
|
|
| [reply] |
|
|
Re: Perl Help
by Slug (Acolyte) on Jan 22, 2008 at 11:25 UTC
|
Perl Monks has been helpful, and I would have struggled for ages without it.
I didn't know much about it when I signed on to it. I had just seen it whilst browsing ages back.
I got the impression from one poster on a previous problem that it was an elitists forum, perhaps he was in a bad mood.
Thanks for the advice.
| [reply] |
Re: Perl Help
by Slug (Acolyte) on Jan 22, 2008 at 11:31 UTC
|
(For coursework)
I've got to write a script which counts certain characters within a text file.
I'm looking for hyphens, open and closed brackets, full stops and commas.
I know how to read a file line by line. I was going to write a regular expression to scan each line, and increment a variable by one for each instance of the character found.
Is there a better method for doing this? | [reply] |
|
|
| [reply] |
|
|
You want something like this:
my %hist;
while(<$fh>){
++$hist{ $_ } for split //;
}
for(sort{ $hist{ $b } <=> $hist{ $a } } keys %hist){
print "'$_' => $hist{ $_ }\n";
}
but you need to filter out the chars you don't want. | [reply] [d/l] |
Re: Perl Help
by Slug (Acolyte) on Jan 22, 2008 at 11:42 UTC
|
Another question? How do I return the amount of a certain character within a string?
I've googled it and found the split command but it seems excessive? | [reply] |
|
|
| [reply] |
|
|
| [reply] |