converting scalar to an array of elements

Discussion in 'Perl Misc' started by Chuckb, Feb 7, 2004.

  1. Chuckb

    Chuckb Guest

    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!
    Chuckb, Feb 7, 2004
    #1
    1. Advertising

  2. Chuckb

    AlV Guest

    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";
    }
    AlV, Feb 7, 2004
    #2
    1. Advertising

  3. 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"
    James Willmore, Feb 7, 2004
    #3
  4. 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
    John W. Krahn, Feb 7, 2004
    #4
  5. In article <>, 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 - <> - 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
    David H. Adler, Feb 9, 2004
    #5
  6. "David H. Adler" wrote:
    >
    > In article <>, 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
    John W. Krahn, Feb 9, 2004
    #6
  7. In article <>, John W. Krahn wrote:
    > "David H. Adler" wrote:
    >>
    >> In article <>, 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 - <> - 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
    David H. Adler, Feb 10, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. helmut januschka

    warning: excess elements in scalar initializer

    helmut januschka, Mar 6, 2004, in forum: C Programming
    Replies:
    5
    Views:
    15,167
    Joe Wright
    Mar 6, 2004
  2. Clint Olsen
    Replies:
    6
    Views:
    354
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    163
    Arndt Jonasson
    Jan 27, 2005
  4. Replies:
    6
    Views:
    108
    A. Sinan Unur
    May 25, 2005
  5. Replies:
    6
    Views:
    84
Loading...

Share This Page