Counting occurences of a char in a line

2005-01-17 Thread Tor Hildrum
Hi,

I have the following code in a script I'm writing:

foreach my $line (INN) {
if ( 10 == ($line =~ s/,/,/g) ) {
print OUT $line;
}
}

Is this poor style? It looks a bit ugly, but I can't figure out a
better way to do it. I'm sure there is :)
The script will be reused and probably maintained by someone else. Is
there a more standard way of doing this?

regards

Tor

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Counting occurences of a char in a line

2005-01-17 Thread Bakken, Luke
 Hi,
 
 I have the following code in a script I'm writing:
 
 foreach my $line (INN) {
 if ( 10 == ($line =~ s/,/,/g) ) {
 print OUT $line;
 }
 }
 
 Is this poor style? It looks a bit ugly, but I can't figure out a
 better way to do it. I'm sure there is :)
 The script will be reused and probably maintained by someone else. Is
 there a more standard way of doing this?

for my $line (INN) {
if ( 10 == ($line =~ tr/,//) ) {
print OUT $line;
}
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Counting occurences of a char in a line

2005-01-17 Thread John W. Krahn
Tor Hildrum wrote:
Hi,
Hello,
I have the following code in a script I'm writing:
foreach my $line (INN) {
if ( 10 == ($line =~ s/,/,/g) ) {
print OUT $line;
}
}
Is this poor style? It looks a bit ugly, but I can't figure out a
better way to do it. I'm sure there is :)
The script will be reused and probably maintained by someone else. Is
there a more standard way of doing this?
That is a Frequently Asked Question.
perldoc -q How can I count the number of occurrences of a substring within a 
string


John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: Counting occurences of a char in a line

2005-01-17 Thread Jenda Krynicky
From: Tor Hildrum [EMAIL PROTECTED]
 I have the following code in a script I'm writing:
 
 foreach my $line (INN) {
 if ( 10 == ($line =~ s/,/,/g) ) {
 print OUT $line;
 }
 }
 
 Is this poor style? It looks a bit ugly, but I can't figure out a
 better way to do it. I'm sure there is :) The script will be reused
 and probably maintained by someone else. Is there a more standard
 way of doing this?

You do not seem to want to count the commas, you seem to want to make 
sure there are exactly ten of them. Which means you might just as 
well use this:

foreach my $line (INN) {
if ( $line =~ /^[^,]*(?:,[^,]*){10}$/) ) {
print OUT $line;
}
}

That is check whether the full string is something not containing 
comma followed by ten times comma and something not containing 
comma.

You may later want to make the regexp a little more restrictive 
later.

Jenda

= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Counting occurences of a char in a line

2005-01-17 Thread Tor Hildrum
On Tue, 18 Jan 2005 01:08:56 +0100, Jenda Krynicky [EMAIL PROTECTED] wrote:

 foreach my $line (INN) {
 if ( $line =~ /^[^,]*(?:,[^,]*){10}$/) ) {
 print OUT $line;
 }
 }
 
 That is check whether the full string is something not containing
 comma followed by ten times comma and something not containing
 comma.

This is a script for an awful system where I only have access to a
certain part of the whole system. It's a web-form with 10 fields.
Instead of putting
the fields directly into a database, I get the fields as a comma-seperated list
on email. One mail for each request(!). 
So, I'm basically parsing a huge mbox-file here.

I have no power over the webmaster, and he won't listen to reason. 
The fields may or may not be empty. So, valid format is:
[EMAIL PROTECTED],bar,foo,bar,foo,bar,foo,bar,foo, bar, foo
and
[EMAIL PROTECTED],,
and
foo,bar,,,

I'm basically just checking for 10 commas, write it to it's own file
and then use a mysql-import script on that file. Any lines with more
than 11 commas get's flagged for manual checking.

To make matters even worse, we are using Matt's Formmail script. I'm
not sure if it's gotten better, but last time I checked it didn't have
the best of track records.

I ended up using tr//, even though it sports almost the same
readability as s// it seems more correct.

Thanks.

Tor

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response