Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > converting scalar to an array of elements

Reply
Thread Tools

converting scalar to an array of elements

 
 
Chuckb
Guest
Posts: n/a
 
      02-07-2004
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!


 
Reply With Quote
 
 
 
 
AlV
Guest
Posts: n/a
 
      02-07-2004
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";


perldoc -f split


use warnings;
use strict;

my $name="Fred";
my @arrayname = split //, $name;
foreach (@arrayname)
{
print $_, "\n";
}
 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      02-07-2004
On Sat, 07 Feb 2004 13:17:33 +0000, 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";


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"

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-07-2004
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.

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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
David H. Adler
Guest
Posts: n/a
 
      02-09-2004
In article <(E-Mail Removed)>, John W. Krahn wrote:
> 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.

dha
--
David H. Adler - <(E-Mail Removed)> - 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-09-2004
"David H. Adler" wrote:
>
> In article <(E-Mail Removed)>, John W. Krahn wrote:
> > 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'.


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
David H. Adler
Guest
Posts: n/a
 
      02-10-2004
In article <(E-Mail Removed)>, John W. Krahn wrote:
> "David H. Adler" wrote:
>>
>> In article <(E-Mail Removed)>, John W. Krahn wrote:
>> > 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

--
David H. Adler - <(E-Mail Removed)> - http://www.panix.com/~dha/
It was when a cow-orker proudly showed me his Java Ring that I finally
realised that Java was nothing more than a huge April Fool joke that had
got out of hand. - Andy Wardley
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting the contents of a scalar cartercc@gmail.com Perl Misc 6 05-05-2006 02:04 PM
how to "spell out" the elements in a scalar variable? minjie@excite.com Perl Misc 6 05-25-2005 04:21 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
warning: excess elements in scalar initializer helmut januschka C Programming 5 03-06-2004 06:02 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments