Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In addition to reviewing the post and accompanying replies linked by davido, please consider the following code. As noted in the previous discussion, split cannot be made to throw an exception if it finds nothing on which to split. Validation of input must be done separately. If you want the process to actually abort on any of the invalid conditions in the code below, replace warn wherever it appears with die (and also remove the associated  and next LINE expressions, although leaving them in will make no difference; they are simply redundant when used with die).

>perl -wMstrict -le "my @lines = ( 'ABCD', 'A|B|C|D', 'A,B,C|D', 'A,B,C', 'A,B,C,D,E', 'A,B,C,D'); ;; LINE: for my $line (@lines) { print qq{processing '$line'}; ;; warn qq{'$line' has no comma} and next LINE if not $line =~ m{,}xms; warn qq{'$line' has nasty pipe} and next LINE if $line =~ m{ \| }xms; ;; warn qq{not exactly 4 fields in '$line'} and next LINE if 4 != (my ($s, $a, $c, $r) = split /,/, $line); ;; print qq{success: s '$s' a '$a' c '$c' r '$r'}; } " processing 'ABCD' 'ABCD' has no comma at -e line 1. processing 'A|B|C|D' 'A|B|C|D' has no comma at -e line 1. processing 'A,B,C|D' 'A,B,C|D' has nasty pipe at -e line 1. processing 'A,B,C' not exactly 4 fields in 'A,B,C' at -e line 1. processing 'A,B,C,D,E' not exactly 4 fields in 'A,B,C,D,E' at -e line 1. processing 'A,B,C,D' success: s 'A' a 'B' c 'C' r 'D'

In reply to Re: split function by AnomalousMonk
in thread split function by Rajsai2825

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-19 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found