The @{[$string]} construction does not correspond to what you want. It evaluates to an array of one element, a copy of the value of $string. I don't quite see what you're attempting with the second try. A string in list context is a one element list.

The perlish way to treat the characters of a string as an array is to produce the array with my @str_ary = split '', $string;, but that does not help you modify the characters of $string in place, as the C char * as array construction would.

You're best bet is with substr. Letting sub fiddle {} take a character and return whatever you wish,

my $index; while ( $index < length( $string) ) { substr( $string, $index, 1) = fiddle(substr( $string, $index, 1)); $index++; }
That isn't nearly as tidy as pointer indexing, but perl is just not as close to the metal as C.

Note that &fiddle can return empty or strings longer than one character, and the changed length will be handled. That is probably an invitation to code with mysterious behaviors, and non-termination is possible.

After Compline,
Zaxo


In reply to Re: Forcing array context by Zaxo
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.