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>


Reply via email to