converting scalar to an array of elements

C

Chuckb

I should know this but, what is the quickest way to convert a scalar value
into an array?
Ex: $name="Fred";
so that
$arrayname[0]="F";
$arrayname[1]="r";
$arrayname[2]="e";
$arrayname[3]="d";

Thanks!
 
A

AlV

Chuckb said:
I should know this but, what is the quickest way to convert a scalar value
into an array?
Ex: $name="Fred";
so that
$arrayname[0]="F";
$arrayname[1]="r";
$arrayname[2]="e";
$arrayname[3]="d";

perldoc -f split


use warnings;
use strict;

my $name="Fred";
my @arrayname = split //, $name;
foreach (@arrayname)
{
print $_, "\n";
}
 
J

James Willmore

I should know this but, what is the quickest way to convert a scalar value
into an array?
Ex: $name="Fred";
so that
$arrayname[0]="F";
$arrayname[1]="r";
$arrayname[2]="e";
$arrayname[3]="d";

Use 'split' :)

This is a simple one liner to produce what you wanted ...

prompt> perl -e '$name="Fred";@chars=split //, $name;print join("\n",
@chars),"\n";'
F
r
e
d
prompt>

Salt to taste :)

HTH


--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Monday, n.: In Christian countries, the day after the baseball
game. -- Ambrose Bierce, "The Devil's Dictionary"
 
J

John W. Krahn

Chuckb said:
I should know this but, what is the quickest way to convert a scalar value
into an array?
Ex: $name="Fred";
so that
$arrayname[0]="F";
$arrayname[1]="r";
$arrayname[2]="e";
$arrayname[3]="d";

I don't know which is the quickest, you'll have to use Benchmark for that.

my @arrayname = split //, $name;

my @arrayname = $name =~ /./sg;

my @arrayname = unpack 'a' x length $name, $name;

my @arrayname = map substr( $name, $_, 1 ), 0 .. length( $name ) - 1;


TMTOWTDI :)

John
 
D

David H. Adler

Chuckb said:
I should know this but, what is the quickest way to convert a scalar value
into an array?
Ex: $name="Fred";
so that
$arrayname[0]="F";
$arrayname[1]="r";
$arrayname[2]="e";
$arrayname[3]="d";

I don't know which is the quickest, you'll have to use Benchmark for that.

[snip various methods]

While we're having fun, how about...

unshift @arrayname, chop $string_containing_Fred while $string_containing_Fred;

....although I have a feeling that may not scale well.

dha
--
David H. Adler - <[email protected]> - http://www.panix.com/~dha/
Your pluck is admirable. However, arguing for a 'pure computer
science' approach in the perl5-porters mailing list is somewhat like
inquiring about mileage in a Maserati dealership. People are given to
drop their champagne glasses and stare. - Felix Gallo, p5p
 
J

John W. Krahn

David H. Adler said:
Chuckb said:
I should know this but, what is the quickest way to convert a scalar value
into an array?
Ex: $name="Fred";
so that
$arrayname[0]="F";
$arrayname[1]="r";
$arrayname[2]="e";
$arrayname[3]="d";

I don't know which is the quickest, you'll have to use Benchmark for that.

[snip various methods]

While we're having fun, how about...

unshift @arrayname, chop $string_containing_Fred while $string_containing_Fred;

...although I have a feeling that may not scale well.

You are going to be a character short in @arrayname if the first
character in $string_containing_Fred is '0'. :)


John
 
D

David H. Adler

David H. Adler said:
Chuckb wrote:

I should know this but, what is the quickest way to convert a scalar value
into an array?
Ex: $name="Fred";
so that
$arrayname[0]="F";
$arrayname[1]="r";
$arrayname[2]="e";
$arrayname[3]="d";

I don't know which is the quickest, you'll have to use Benchmark for that.

[snip various methods]

While we're having fun, how about...

unshift @arrayname, chop $string_containing_Fred while $string_containing_Fred;

...although I have a feeling that may not scale well.

You are going to be a character short in @arrayname if the first
character in $string_containing_Fred is '0'. :)

I am insulted that you would think I would use an inappropriate variable
name. Clearly, you are thinking of $string_containing_0_and_stuff :)

dha
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top