in reply to Re: Perl split on regex match
in thread Perl split on regex match

Hi Athanasius

Why do you print join ',', @cols when you already have $line in scope, with the complete line in it?

I would be tempted to drop the chomp too, as we are never looking that far down the line. Then we have this code...

use strict; use warnings; while (my $line = <DATA>) { my @cols = split /,/, $line; my @third = split /\./, $cols[2]; print $line if @third < 3; }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^3: Perl split on regex match
by GotToBTru (Prior) on Jan 03, 2017 at 15:17 UTC
    while (my $line = <DATA>) { print $line if (split /\./,$line) < 3; }
    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      while (<DATA>) { print if (split /\./) < 3; }
      (but use a CSV parser!)


      The way forward always starts with a minimal test.

      That is assuming the '.' will not occur in any of the other fields.

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!

        An assumption supported by all supplied test data. The #1 objection I have with my post is it continues the bad practice of not using a proper CSV module. Overkill, if the example data is truly representative of all this program will ever have to deal with, but of course that's rarely the case. Assuming only the one column will contain periods is the least of my sins.

        And, as 1nickt demonstrated, even my oversimplification could be simplified further.

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)