I think the best way is
What's your definition of "best way"? split // seems to be the most common idiom. And the "ugly" way, use of substr seems to be the fastest according to the following benchmark:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw /timethese cmpthese/; our $char; for my $size (4, 16, 64, 256, 1024) { print "Iterating over a string of length $size.\n"; our $str = "-" x $size; cmpthese -5 => { split => 'for my $c (split // => $str) {$char = $c}', for => 'for my $c ($str =~ /(.)/g) {$char = $c}', while => 'while ($str =~ /(.)/g) {$char = $1}', substr => 'for my $i (0 .. (length ($str) - 1)) {$char = substr $str +, $i, 1}', } } __END__ Iterating over a string of length 4. Rate split for while substr split 157193/s -- -9% -20% -44% for 173109/s 10% -- -12% -38% while 197371/s 26% 14% -- -30% substr 280339/s 78% 62% 42% -- Iterating over a string of length 16. Rate split for while substr split 47828/s -- -3% -12% -55% for 49213/s 3% -- -10% -54% while 54413/s 14% 11% -- -49% substr 107090/s 124% 118% 97% -- Iterating over a string of length 64. Rate split for while substr split 12882/s -- -5% -7% -56% for 13574/s 5% -- -2% -54% while 13798/s 7% 2% -- -53% substr 29595/s 130% 118% 114% -- Iterating over a string of length 256. Rate split for while substr split 3244/s -- -5% -7% -59% for 3420/s 5% -- -2% -57% while 3483/s 7% 2% -- -56% substr 7951/s 145% 132% 128% -- Iterating over a string of length 1024. Rate split for while substr split 818/s -- -4% -7% -59% for 850/s 4% -- -3% -58% while 875/s 7% 3% -- -57% substr 2013/s 146% 137% 130% --
I must say, the performance of substr surprises me, and I find the difference between substr and other methods suspect, but I can't find any flaw in the benchmark.

Abigail


In reply to Re: foreach (each character in string..)? by Abigail-II
in thread foreach (each character in string..)? by Anonymous Monk

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.