Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This evil goes against my sense of sane coding, but it seems to be what you're asking for:

use strict; use warnings; while( my $line = <DATA> ) { chomp $line; my( $a, $b, $c, $d ) = split /(?(?=^[^|]*\|)(?{die "Pipe [|] detected in input."})|)[,\ +t]/, $line; print "[($a)($b)($c)($d)]\n"; } __DATA__ ABC,DEF,GHI,JKL ABC|DEF|GHI|JKL

This throws an exception from within the regex passed to split if the input string contains a pipe character. I wouldn't recommend bringing that to a code review, but given that none of the other solutions already provided seem to satisfy you, I am thinking that you'll only be happy when an exception is thrown as part of the split line. Despite the hackish nature of the code, it produces what you're requesting. Here's the output:

[(ABC)(DEF)(GHI)(JKL)] Pipe [|] detected in input. at (re_eval 1) line 1, <DATA> line 2.

It would be a lot better to just follow the advice of bart's post, or Colonel_Panic's post, in this same thread. And if neither of those posts does what you need, rather than just repeating your question again, explain exactly how their code fails to meet your needs. I find it hard to believe that your requirement is for the exact line containing the split to throw an exception. It seems a lot more reasonable to just assure that an exception is thrown once split fails to produce reasonable output, or possibly to pre-screen the line of text and throw before you split, if a pipe character is found.

Update: Just for fun, an explanation of the regex:

(?(condition)true_regex|false_regex) creates a conditional. For our condition, we use a zero-width lookahead assertion, (?=^[^|]*|) that detects if a pipe character is found anywhere in the string. If that condition is satisfied, the "true_regex" gets tested. The "true_regex" that we use is a (?{code}) construct, which is used (or abused) to execute Perl code from within a regular expression. The codeabuse we execute is the die statement. For our "false_regex", we use an empty expression, which will not affect the rest of the split match. The remainder of the regex is just what we would normally pass to 'split'.


In reply to Re^3: Split function by davido
in thread Split function by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (5)
    As of 2018-05-25 21:01 GMT
    Find Nodes?
      Voting Booth?