Re: converting scalar to an array of elements

From: John W. Krahn (krahnj_at_acm.org)
Date: 02/08/04


Date: Sat, 07 Feb 2004 23:54:20 GMT

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


Relevant Pages

  • Re: converting scalar to an array of elements
    ... > I should know this but, what is the quickest way to convert a scalar value ... > into an array? ... perldoc -f split ...
    (comp.lang.perl.misc)
  • Re: converting scalar to an array of elements
    ... In article, John W. Krahn wrote: ... > Chuckb wrote: ... >> I should know this but, what is the quickest way to convert a scalar value ... > I don't know which is the quickest, you'll have to use Benchmark for that. ...
    (comp.lang.perl.misc)
  • Sorting a dynamic array
    ... I'm interested in finding out the quickest and simplest way to sort a ... I'd like to write a subroutine that can be called with 3 input ... ARG= Array to be sorted (in which each attribute represents a line ... ARG= Multivalued list of columns on which sort is to operate ...
    (comp.databases.pick)
  • Re: Summing numbers from a list to a goal
    ... I am trying to figure out the quickest way to do the following. ... You are given a list of integers, a goal sum, and an ... If sorted, I might perhaps take the optimistic view that, on average, the number would be found fastest by starting at the median number and adding numbers on either end of the array and then adding it and repeating with next median. ...
    (comp.programming)
  • Re: [PHP] PHP Serialization Performance
    ... that are needed by my application, returned in an array. ... I spent awhile trying to look at the performance of php serialization, ... Not taking into account the actual performance, the quickest to implement would be a combination of serialize, unserialize, file_put_contentsand file_get_contents. ... Using these functions you could very quickly create a persistent cache for data. ...
    (php.general)