Convert a string into array of characters

Convert a string into array of characters

am 27.02.2006 22:15:20 von 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?

--
http://DavidFilmer.com

Re: Convert a string into array of characters

am 27.02.2006 22:40:58 von 1usa

usenet@DavidFilmer.com wrote in news:1141074920.245948.5590
@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__


--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines .html

Re: Convert a string into array of characters

am 27.02.2006 22:54:42 von Jake Peavy

usenet@DavidFilmer.com wrote:
> 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

Re: Convert a string into array of characters

am 27.02.2006 23:06:17 von tassilo.von.parseval

Also sprach usenet@DavidFilmer.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?

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
--
use bigint;
$n=714233503437702801613970263303373711390544118542200534375 65440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);

Re: Convert a string into array of characters

am 27.02.2006 23:09:31 von Jake Peavy

Tassilo v. Parseval wrote:
> my @chars = unpack "(a)*", $string;

no one told me about the ()'s!

very nice :)

please ignore my previous attempt.

-jp

Re: Convert a string into array of characters

am 28.02.2006 00:00:43 von tassilo.von.parseval

Also sprach DJ Stunks:

> Tassilo v. Parseval wrote:
>> my @chars = unpack "(a)*", $string;
>
> 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
--
use bigint;
$n=714233503437702801613970263303373711390544118542200534375 65440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);

Re: Convert a string into array of characters

am 28.02.2006 05:44:17 von someone

A. Sinan Unur wrote:
> usenet@DavidFilmer.com 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