I have a small section of code that works. But I would like it to be faster. The size of the data will stay around its current size to maybe four times that. If that is any help in optimizing the code. Here is the working code in a Benchmark test.

use strict; use warnings; use Data::Dumper; use Benchmark; # verification pattern is used by the developer to set a range # in this case the first letter before the slash is to verify # the attribute name in $attrs. The content after the slash # is used to verify the attribute was not used yet. but in the full # version ere is a range that goes to a function to verify the # attribute values. my $match_list = 'a/ere,c/ere,d/ere'; # in $attrs the letter before equal is the attribute name # and needs to be compared with $match_list for its value to be replac +ed. # all attribute names can only be used one time in $attrs to be good # the value of the attribute may have a space in it. my $attrs = 'TY a=errt c=rrr dd d=rrr dd'; # example that should conve +rt #my $attrs = 'TY a=errt c=rrr dd d=rrr d=du 0'; # example that should +say "nope" # tag name, just something that is there. my $tag = 'TY'; # markup is the template of the output if its good my $markup = 'TY:X{a} X{c}|X{d}'; # few more examples based off some of your posts sub third_draft { my %xlist = map{ split('\/', $_) } split(',', $match_list); $attrs =~ s/\A$tag //; # clean up for (split (' (?=\w+\=)', $attrs)) { my ($name, $value) = split('='); # only one attribute of the same name is allowed. if (defined $xlist{$name}) { $xlist{$name} = undef; # this exists now $markup =~ s/X\{$name\}/$value/g; } else { # fails get nothing and end. $markup = ''; last; } } # not in Benchmark # $markup # ? print $markup # : print 'nope'; } sub second_draft { my %xlist = map{ split(/\//, $_) } # make list split(/,/, $match_list); $attrs =~ s/\A$tag //; # clean up my @attr = $attrs =~ /(?:\A| )(.+?)(?=(?: \w+=|\z))/g; # make arra +y for xlist for (@attr) { my ($name, $value) = split(/=/,$_); # only one attribute of the same name is allowed. if (exists $xlist{$name} && defined $xlist{$name}) { $xlist{$name} = undef; # this exists now $markup =~ s/X\{$name\}/$value/g; } else { # fails get nothing and end. $markup = ''; last; } } # not in Benchmark # $markup # ? print $markup # : print 'nope'; } sub first_draft { my %xlist = map{ split(/\//, $_, 2) } # make list grep{ m/\// } split(/,/, $match_list); $attrs =~ s/\A$tag //; # clean up my @attr = $attrs =~ /(?:\A| )(.+?)(?=(?: \w+=|\z))/g; # make arra +y for xlist for (@attr) { my ($name, $value) = split(/=/,$_); # only one attribute of the same name is allowed. if (exists $xlist{$name} && defined $xlist{$name}) { $xlist{$name} = undef; # this exists now $markup =~ s/X\{$name\}/$value/g; } else { # fails get nothing and end. $markup = ''; last; } } # not in Benchmark # $markup # ? print $markup # : print 'nope'; } timethese($ARGV[0] || -1, { 1 ? ( 'a_first_draft' => \&first_draft, ) : (), 1 ? ( 'b_second_draft' => \&second_draft, ) : (), 1 ? ( 'c_third_draft' => \&third_draft, ) : (), });
Thank you in advance.

Update: Added better comments in code.


In reply to Speeding up a Perl code. Can it be faster? by $h4X4_|=73}{

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.