Hi All,

I want to find a way to parse a string in a real performant way. This is what I came up with.
I found 2 methods so far, but there must be a better way to do it.
The string is position delimited, i.e. from the 3the to the 16the it contains something, and so far. Now, I want only the text in these fields, not the blanks.
Let me show you

our @data=<DATA>; # some code comes here... sub dosubstr { foreach my $i ( 0..$#data ) { my $line=$data[$i]; my $jcpu=substr($line,2,16); my $j=substr($line,18,48); my $s=substr($line,290,16); $jcpu =~ s/\s//g; $s =~ s/\s//g; $j =~ s/\s//g; # warn "$jcpu $j $s"; # ..store the values in a hash, but that is not important here } } sub doregex { foreach my $i ( 0..$#data ) { my $line=$data[$i]; $line =~ m/^04(?=(\S+)).{16}(?=(\S+)).{40}.{216}.{16}(?=(\S+)).{ +16}/ ; my $jcpu=$1; my $j=$2; my $s=$3; # warn "$jcpu $j $s"; # ..store the values in a hash, but that is not important here } } __DATA__ 04A12345 RELEASE A12345 RELEASE A12345 04FTOP DD_BUIL+ FTOP DD_REKL+ FTOP 04FTOP DD_PLAN+ FTOP DD_REKL+ FTOP
Now, in a simple benchmark study, the doregex function is 3 times faster than the substr. But it just look so complex doesn't it.
So, I am asking the wisdom for my fellow monks to make it more performant.
I am talking about a data of thousands of lines and every second counts, as my operators don't like to wait for webpages :-) Thanks in advance,
Update: fixed substr value to correct Abigail-II comment
---------------------------
Dr. Mark Ceulemans
Senior Consultant
BMC, Belgium

In reply to fast string parser: regex versus substr by mce

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.