In order to understand what is happening, you need to know how split works. split is used to split a string into fields based on a separator character or characters. For example, split(/,/, "Bob,17,M") returns the list ('Bob', '17', 'M') because the commas are treated as separators, and the text between them as fields.
When the regex includes capturing parens, the captured separators are returned along with the fields, so split(/(,)/, "Bob,17,M") returns ('Bob', ',', '17', ',', 'M').
split /(..)/, '00a0c801adc6') treats each pair of characters as a separator, and the text between each pair of characters as a field. Of course, there is no text between the characters, so you get null strings for the fields: ('', '00', '', 'a0', '', 'c8', '', '01', '', 'ad', '', 'c6'). (The trailing empty fields are discarded.) When you join that list back together, you get extra colons because of all the null strings.
When you want to grab groups of characters from a string without separators, instead of split you can use a plain old regex match with /g:
print join(':', '00a0c801ad' =~ /../g), "\n<BR>";
-
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.
|