Actually I would say that your restriction on using regexes is a mistake. The reason I say this is that regexes are essentially the perl route to manipulating strings in the way that you want to. Using substr() is a possibility but it is not anywhere near as powerful or efficient as a regex.

For instance if you want to iterate over the characters of the string, performing some kind of transformation upon them you probably want to do something like

s/(.)/ord($1)>127 or ord($1)<=32 ? sprintf("%%%02x",ord($1)) : $1/se +;
The s/// operator is about the best way in perl to iterate over all of the chars in a string (and be able to modify them in place, other means are available if you dont want in place editing) with something close to the efficiency of C. This is primarily because s/(.)/(whatever)/se is roughly equivelent to (using the above example instead of the ...)
for my $ofs (0..length($str)-1) { my $c=substr($str,$ofs,1); substr($str,$ofs,1)=sprintf("%%%02x",ord($c)) if ord($c)>127 or ord( +$c)<=32; }
But all of the iteration code, the repeated substr()s etc are all done in the C layer and not in the interpreted opcode layer which means it is much more efficient.

Before you ask why I don't just do this in C, it's because I want to see if it can be done in Perl.

Perl is a GPPL/Turing Complete. Thus it can do _anything_ that any other turing complete/GPPL language can do. And of course C is turing complete....

The real question is how fast was it to write, how maintainable is it, how fast is it to run, and how much did it cost to write. Normally Perl outperforms C when all criteria are taken together. Of course there some things that are much easier in C. But iterating over a character array isnt really one of them.

--- demerphq
my friends call me, usually because I'm late....


In reply to Re: Forcing array context by demerphq
in thread Forcing array context by jens

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.