Re: Convert a string into array of characters



A. Sinan Unur wrote:
usenet@xxxxxxxxxxxxxxx wrote in news:1141074920.245948.5590
@i39g2000cwa.googlegroups.com:

I can do this to convert a scalar string into an array of individual
characters:

my @characters = split (undef, $string); #or ('', $string)

But if I get close to my screen, I detect a faint odor of bad code. Is
there a better way?

Is there something you want to apply to each character in return?

Would iterating over each character be acceptable?

#!/usr/bin/perl

use warnings;
use strict;

my $foo = 'ffooooo';

do_something($1) while $foo =~ m{(.)}g;

To be equivalent you would have to include the /s option:

do_something($1) while $foo =~ m{(.)}sg;


And you don't really need the capturing parentheses:

do_something($_) for $foo =~ m{.}sg;



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: Regular expression to define valid MVS dataset
    ... FOO) from zero to 21 times (21 times making ... First character is Alphabetic or #$@ ... For IBM-MAIN subscribe / signoff / archive access instructions, ... send email to listserv@xxxxxxxxxxx with the message: GET IBM-MAIN INFO ...
    (bit.listserv.ibm-main)
  • Re: Slash problem in simple sed/awk/perl job
    ... I want to use sed or awk or perl to replace a token in a file with the ... % sed s/TOKEN/$VAR/ foo ... so character 10 is the "h" which is not a legal option character ...
    (comp.unix.shell)
  • Re: keyword question
    ... `(: foo) ... consisting of only that character. ... This version of ACL is definitely wrong. ... SBCL interpretation of the ...
    (comp.lang.lisp)
  • Question on stack frame allocation space
    ... void foo() { ... If I just add one more character (so that I'd be copying 41 ... this confirms that the stack frame for "foo" has allocated 40 bytes for ... I wanted to test the "buffer overflow bug", and tried to overwrite the ...
    (comp.lang.c)
  • Re: conditions on catcodes
    ... Anyone, a clean, general way to make a conditional statement based on ... the \catcode of a character? ...
    (comp.text.tex)