note
Flexx
<p>[invaderzard], just wanted to make clear that this solution by [Kenosis] is the far quicker and easier version, which I'd, of course use anytime I'd just need a quick split by a field separator on an input.</p>
<p>But: There is one caveat here to keep in mind. Split, of course, does not test the format of the input. So if you wanted the second field of a record that goes like this:</p>
<code>
$record = 'A;B;C;D';
</code>
then
<code>
$second_field = (split ';', $record)[1];
</code>
<p>does work. However so it does for inputs like:</p>
<code>
A;B
#foo;B;ar
;B
</code>
<p>All of the above inputs would leave a <code>B</code> in <code>$second_field</code>. Which, you know might be correct in a particular case, but in general, we don't want to just ignore malformed records, so if we, say, iterate over records, then make sure to test and capture using a regexp in an if:
</p>
<code>
if($record =~ m/^.;(.);.;.$/) {
$second_field = $1;
}
</code>
<p>Now this will only set <code>$second_field</code> if the record matches the four single-character fields delimited by one semicolon format. Even if the input is <code>';;;;;;;'</code>. ;)</p>
<p>Have fun with regexen. They're cool. ;)</p>
<p>So long,<br/>
[Flexx]</p>
</div></div>
986543
986550