That gives an off-by-one $tokens value (it's actually counting the stuff "around" the tokens (update: and it requires creation of an otherwise unused array to hold most of that stuff)), but that's easy to fix:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le
"my $t = 'x';
;;
for my $line (qw(
1212123x534534534534xx4545454x232322xx
0901001x876879878787xx0909918x212245xx
1212123x534534534534xx4545454x232323xx
1212133x534534534534xx4549454x232322xx
4352342xx23232xxx345545x45454x23232xxx
)) {
my $tokens = my ($first, @rest) = split $t, $line, -1;
$tokens -= 1;
print qq{'$line': num '$t' tokens is: $tokens};
dd ($first, \@rest);
}
"
'1212123x534534534534xx4545454x232322xx': num 'x' tokens is: 6
(1212123, [534534534534, "", 4545454, 232322, "", ""])
'0901001x876879878787xx0909918x212245xx': num 'x' tokens is: 6
("0901001", [876879878787, "", "0909918", 212245, "", ""])
'1212123x534534534534xx4545454x232323xx': num 'x' tokens is: 6
(1212123, [534534534534, "", 4545454, 232323, "", ""])
'1212133x534534534534xx4549454x232322xx': num 'x' tokens is: 6
(1212133, [534534534534, "", 4549454, 232322, "", ""])
'4352342xx23232xxx345545x45454x23232xxx': num 'x' tokens is: 10
(
4352342,
["", 23232, "", "", 345545, 45454, 23232, "", "", ""],
)
(But I don't really see anything wrong with using good old
tr/// for counting and poor old
substr for fixed-field extraction.)
Update: This gets rid of @rest and the $tokens -= 1; statement for all you one-liner addicts out there:
my $tokens = (my ($first) = split $t, $line, -1) - 1;
Give a man a fish: <%-{-{-{-<
-
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.