I have six commas in the file and when I do this code...
while ($line = <IN>)
{
if($line =~ m/[\,]/){$comma++;}
}
print ("$comma");
It says I have four. I have two period characters and when I ammend the code to recognise the periods...
if($line =~ m/[\.]/){$sen++;}
It correctlys says I have just two periods. So how would I make the file recognise the commas?
[edited by: phranque at 2:04 am (utc) on Jan. 19, 2009]
[edit reason] disabled graphic smileys ;) [/edit]
$comma = 0;
while ($line = <IN>) {
$comma++ while ($line =~ /,/g);
}
print $comma;
the better way is like this:
$comma = 0;
while ($line = <IN>) {
$comma += $line =~ tr/,/,/;
}
print $comma;
You should try and use regular expressions for matching patterns. Counting a character is not a pattern, so using the tr/// operator is way more efficient.
[edited by: phranque at 9:23 am (utc) on Jan. 19, 2009]
[edit reason] disabled graphic smileys ;) [/edit]
[perldoc.perl.org...]