Convert a string into array of characters

U

usenet

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?
 
A

A. Sinan Unur

(e-mail address removed) wrote in @i39g2000cwa.googlegroups.com:
Subject: Convert a string into array of characters

Now, why do you want that?
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;

sub do_something {
print shift, "\n";
}

__END__
 
D

DJ Stunks

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?

since that behaviour was intentionally added to split, I assume it's
the best way to perform that operation, however I would normally have
written

my @chars = split //,$string;

that said, however, you could use pack & unpack if so inclined:

C:\tmp>perl -e "$_='this is a test'; \
@ary = map {pack 'C',$_} unpack 'C*',$_; \
print join ':',@ary"
t:h:i:s: :i:s: :a: :t:e:s:t

-jp
 
T

Tassilo v. Parseval

Also sprach (e-mail address removed):
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?

I suppose the

split undef, $string;

smells a little. It's more common and totally acceptable to do:

my @chars = split //, $string;

This is probably the canonical way. A little faster might be:

my @chars = unpack "(a)*", $string;

Of course, unpack tends to have that odor of obscurity and obfuscation
to some people. Also to me sometimes.

Tassilo
 
T

Tassilo v. Parseval

Also sprach DJ Stunks:
no one told me about the ()'s!

very nice :)

I think the documentation for pack/unpack isn't blatantly clear about
this feature. I just had another skim through them and didn't I already
know of it I wouldn't be able to deduce it from the docs.

I probably learnt of it by trying to transfer the concept of capturing
parents from regexps to unpack templates. Since these two have not much
to do with each other, it was pure happenstance that I found it.

Tassilo
 
J

John W. Krahn

A. Sinan Unur said:
(e-mail address removed) wrote in @i39g2000cwa.googlegroups.com:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top