Another way without using substr (which is actually seldom used in Perl) is to use split, like a simple CSV file would be parsed, except with 'x' instead of ','.
#!usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
while (my $line =<DATA>)
{
chomp $line;
print "line = $line\n";
my $tokens =(my $first, my @rest)= split 'x',$line,-1;
print "num tokens is: $tokens\n";
print Dumper $first, \@rest;
print "\n";
}
=prints
line = 1212123x534534534534xx4545454x232322xx
num tokens is: 7
$VAR1 = '1212123';
$VAR2 = [
'534534534534',
'',
'4545454',
'232322',
'',
''
];
line = 0901001x876879878787xx0909918x212245xx
num tokens is: 7
$VAR1 = '0901001';
$VAR2 = [
'876879878787',
'',
'0909918',
'212245',
'',
''
];
line = 1212123x534534534534xx4545454x232323xx
num tokens is: 7
$VAR1 = '1212123';
$VAR2 = [
'534534534534',
'',
'4545454',
'232323',
'',
''
];
line = 1212133x534534534534xx4549454x232322xx
num tokens is: 7
$VAR1 = '1212133';
$VAR2 = [
'534534534534',
'',
'4549454',
'232322',
'',
''
];
line = 4352342xx23232xxx345545x45454x23232xxx
num tokens is: 11
$VAR1 = '4352342';
$VAR2 = [
'',
'23232',
'',
'',
'345545',
'45454',
'23232',
'',
'',
''
];
=cut
__DATA__
1212123x534534534534xx4545454x232322xx
0901001x876879878787xx0909918x212245xx
1212123x534534534534xx4545454x232323xx
1212133x534534534534xx4549454x232322xx
4352342xx23232xxx345545x45454x23232xxx
-
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.