in reply to Illegal division by zero
One problem:
When the last two digits of the line are '10', why is only '0' captured? The .* "consumes" as much as possible of non-newline stuff (including digits), the \d is required for a match, but the \d? is not, so only one digit is captured. Try something like:c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $x = 'CA006139520,\"WINDSOR RIVERSIDE, ON CA\",2018-01-02,10'; ;; my @windsordigits; @windsordigits = $x =~ /WINDSOR\sRIVERSIDE.*(\d\d?)/; dd \@windsordigits; " [0]
to get all digits at the end of the line. (Update: If the line may end in an un-chomp-ed newline, use \Z (big-Z) instead of \z (little-z) as the end-of-line anchor.)c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $x = 'CA006139520,\"WINDSOR RIVERSIDE, ON CA\",2018-01-02,10'; ;; my @windsordigits; @windsordigits = $x =~ /WINDSOR\sRIVERSIDE.*\b(\d+)\z/; dd \@windsordigits; " [10]
Another problem:
You're assigning a single item to the array on each pass through the while-loop; the array will never have more than a single item in it no matter how many lines you read. Try something like@windsordigits = ...;
(Update: E.g.:push @windsordigits, $x =~ /WINDSOR\sRIVERSIDE.*\b(\d+)\z/;
Note use of \Z anchor.)c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @windsordigits; ;; for my $x ( 'CA006139520,\"WINDSOR RIVERSIDE, ON CA\",2018-01-02,10', qq{CA006139520,\"WINDSOR RIVERSIDE, ON CA\",2018-01-02,987\n}, qq{CA006139520,\"WINDSOR RIVERSIDE, ON CA\",2018-01-02,6\n}, ) { push @windsordigits, $x =~ /WINDSOR\sRIVERSIDE.*\b(\d+)\Z/; } dd \@windsordigits; " [10, 987, 6]
See the flip-flop operator .. in perlop for help with the line-range problem. (Update: See use of range operator .. "As a scalar operator ..." in perlop.)
Give a man a fish: <%-{-{-{-<
|
|---|