Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^6: Suggestions to make this code more Perlish

by TheDamian (Priest)
on Mar 31, 2014 at 07:40 UTC ( #1080365=note: print w/replies, xml ) Need Help??

in reply to Re^5: Suggestions to make this code more Perlish
in thread Suggestions to make this code more Perlish

What I haven't figured out yet is why I was getting zero \037 characters at the end (when I changed ',?' to '(?:,|\000)' in the second solution).

My apologies for misinterpreting your implied question.

The reason your second solution is producing zero trailing "\037" characters is because (?:,|\000) can never match nothing. It either matches a trailing comma, or a trailing null character. So on the very last field (which has neither a trailing comma nor a trailing null-byte), your field pattern wasn't matching at all, so you were not rewriting the last field at all, hence no extra "\037" was added after it.

And, because that final field failed to match, the global matching sequence was terminated at that point, so the regex didn't do that one extra "match an empty field at the end" iteration, which was previously adding the second "\037".

Technically, the use of '(?:,|\000)' introduced a bug, as it would then treat any embedded null as a field separator. Granted, it is quite unlikely to find an embedded null in a CSV file, but not impossible.

If you wanted to keep using this approach, you could avoid that nasty edge-case by replacing the (?:,|\000) subpattern with a simple comma:

    my $re = qr{ (?: "(?<a>[^"]*)" | (?<a>[^,]*) ), }x;


Replies are listed 'Best First'.
Re^7: Suggestions to make this code more Perlish
by kcott (Bishop) on Mar 31, 2014 at 07:56 UTC

    ++ Thankyou very much.

    Once explained, it now seems obvious. One of those "need a fresh set of eyes" situations.

    My focus had been on why '$+{a}' was being replaced instead of '$+{a}\037'. Of course, no replacement is taking place at all. Doh!

    -- Ken

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1080365]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2022-05-27 15:32 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (95 votes). Check out past polls.