FAQ
say I have array:
@a=["I","LOVE","PERL"]
I would like to make a scalar:
$b="I Love Perl\n"

can I do this with a simple one-liner?

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

Search Discussions

  • PANNEER SELVAN at Jul 1, 2003 at 8:11 am
    Is this okay?

    @a=("I","LOVE","PERL");
    $i=join(" ",@a);
    print $i;


    ----- Original Message -----
    From: "Ling F. Zhang" <lingfengz@yahoo.com>
    To: <beginners@perl.org>
    Sent: Tuesday, July 01, 2003 4:56 PM
    Subject: combining array to scalar

    say I have array:
    @a=["I","LOVE","PERL"]
    I would like to make a scalar:
    $b="I Love Perl\n"

    can I do this with a simple one-liner?

    __________________________________
    Do you Yahoo!?
    SBC Yahoo! DSL - Now only $29.95 per month!
    http://sbc.yahoo.com

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • Ling F. Zhang at Jul 1, 2003 at 10:47 am
    Yeah, thanx, I figure this one out after I realize the
    opposite of split is join...how stupid was I not
    recognizing that!

    --- PANNEER SELVAN wrote:
    Is this okay?

    @a=("I","LOVE","PERL");
    $i=join(" ",@a);
    print $i;


    ----- Original Message -----
    From: "Ling F. Zhang" <lingfengz@yahoo.com>
    To: <beginners@perl.org>
    Sent: Tuesday, July 01, 2003 4:56 PM
    Subject: combining array to scalar

    say I have array:
    @a=["I","LOVE","PERL"]
    I would like to make a scalar:
    $b="I Love Perl\n"

    can I do this with a simple one-liner?

    __________________________________
    Do you Yahoo!?
    SBC Yahoo! DSL - Now only $29.95 per month!
    http://sbc.yahoo.com

    --
    To unsubscribe, e-mail:
    beginners-unsubscribe@perl.org
    For additional commands, e-mail:
    beginners-help@perl.org

    __________________________________
    Do you Yahoo!?
    SBC Yahoo! DSL - Now only $29.95 per month!
    http://sbc.yahoo.com
  • Sudarshan Raghavan at Jul 1, 2003 at 8:15 am

    Ling F. Zhang wrote:
    say I have array:
    @a=["I","LOVE","PERL"]
    You have created a reference to an anonymous array here, I guess you meant
    @a = qw/I LOVE PERL/;
    I would like to make a scalar:
    $b="I Love Perl\n"
    Please don't name variables as $b etc, they make the code unreadable and
    read through this doc (perldoc -f sort)
    This will do what you want
    my $arr_to_str = "@a";
    can I do this with a simple one-liner?

    __________________________________
    Do you Yahoo!?
    SBC Yahoo! DSL - Now only $29.95 per month!
    http://sbc.yahoo.com

  • Kevin Pfeiffer at Jul 1, 2003 at 9:38 am
    In article <3F0144F7.5030405@kyocera-wireless.com>, Sudarshan Raghavan
    wrote:
    Ling F. Zhang wrote:
    say I have array:
    @a=["I","LOVE","PERL"]
    [...]
    This will do what you want
    my $arr_to_str = "@a";
    [...]

    Wow!

    I tried a couple more variations - and wonder why the last one doesn't
    evaluate a count of the slice...

    #!/usr/bin/perl
    use warnings;
    use strict;

    my @array = qw/apples oranges pears/;

    my $string = "@array";
    print "$string\n";
    # prints "apples oranges pears"

    $string = join '', @array;
    print "$string\n";
    # prints "applesorangespears"

    $string = "@array[0..1]";
    print "$string\n";
    # prints "apples oranges"

    $string = @array[0..1];
    print "$string\n";
    # prints "oranges" (why not "2"?)

    -K
    --
    Kevin Pfeiffer
    International University Bremen
  • Sudarshan Raghavan at Jul 1, 2003 at 10:05 am

    Kevin Pfeiffer wrote:
    In article <3F0144F7.5030405@kyocera-wireless.com>, Sudarshan Raghavan
    wrote:


    Ling F. Zhang wrote:


    say I have array:
    @a=["I","LOVE","PERL"]
    [...]

    This will do what you want
    my $arr_to_str = "@a";
    [...]

    Wow!

    I tried a couple more variations - and wonder why the last one doesn't
    evaluate a count of the slice...

    #!/usr/bin/perl
    use warnings;
    use strict;

    my @array = qw/apples oranges pears/;

    my $string = "@array";
    print "$string\n";
    # prints "apples oranges pears"
    What I should also have mentioned is that the interpolation is done
    using the $" scalar. It defaults to a single space. But, it can be
    changed. I guess the safest approach would be
    my $arr_to_str = join (' ', @a);

    perldoc -f join
    perldoc perlvar #For info on $"
    $string = join '', @array;
    print "$string\n";
    # prints "applesorangespears"


    $string = "@array[0..1]";
    print "$string\n";
    # prints "apples oranges"

    $string = @array[0..1];
    print "$string\n";
    # prints "oranges" (why not "2"?)
    array vs list, an array in a scalar context evaluates to the number of
    elements in it. The same does not apply to a list, infact I don't think
    there is a list in scalar context.

    my $string = ('one', 'two', 'three'); #this will result in $string
    containing 'three'
    Reason: What applies here is the comma operator, the result of the
    expression is the result of the last sub-expression.

    @array[0..1] evaluates to a two element list ("apples", "oranges"). When
    this is assigned to a scalar, the result will be "oranges" (the last
    sub-expression)
    -K
  • Steve Grazzini at Jul 1, 2003 at 11:07 am

    On Tue, Jul 01, 2003 at 03:43:31PM +0530, Sudarshan Raghavan wrote:
    array vs list, an array in a scalar context evaluates to the
    number of elements in it. The same does not apply to a list,
    infact I don't think there is a list in scalar context.
    Yep. "There is no list... "
    my $string = ('one', 'two', 'three');
    #this will result in $string containing 'three'
    Reason: What applies here is the comma operator, the result
    of the expression is the result of the last sub-expression.
    Exactly. In scalar context the comma operator returns
    (the result of) its right-hand side, so a series of commas
    will return the last item in the series.

    But in this case, Perl knows in advance that all the elements
    of the thing-that-looks-like-a-list are constants and that the
    first two words are going to be discarded. So it just throws
    them away at compile-time and runs this simplified version:

    my $string = 'three';

    Deparse will show you something cute here:

    $ perl -MO=Deparse -e '$x = (1,2,3)'
    -e syntax OK
    $x = ('???', '???', 3);
    @array[0..1] evaluates to a two element list ("apples", "oranges").
    When this is assigned to a scalar, the result will be "oranges"
    (the last sub-expression)
    But no! All together now: "There is no list."

    I don't think *any* of the builtin operators will return a list
    in scalar context. (Although subroutines can do so.)

    Here the "array slice" operator is aware of the scalar context
    and it (arbitrarily) returns the last element. It could just as
    easily have returned the first element, or the count, or anything
    else.

    I don't mean that it's unpredictable, just that there's no way
    to explain *why* it returns the last element. That's just what
    it does. Perhaps the rationale in this particular case is that
    people are often doing:

    print "The answer is: ", @a[1] + @b[1];

    And they'd run away screaming if they got "2" every time. :-)

    HTH
    --
    Steve
  • Sudarshan Raghavan at Jul 1, 2003 at 11:27 am

    Steve Grazzini wrote:
    On Tue, Jul 01, 2003 at 03:43:31PM +0530, Sudarshan Raghavan wrote:

    array vs list, an array in a scalar context evaluates to the
    number of elements in it. The same does not apply to a list,
    infact I don't think there is a list in scalar context.
    Yep. "There is no list... "


    my $string = ('one', 'two', 'three');
    #this will result in $string containing 'three'
    Reason: What applies here is the comma operator, the result
    of the expression is the result of the last sub-expression.
    Exactly. In scalar context the comma operator returns
    (the result of) its right-hand side, so a series of commas
    will return the last item in the series.

    But in this case, Perl knows in advance that all the elements
    of the thing-that-looks-like-a-list are constants and that the
    first two words are going to be discarded. So it just throws
    them away at compile-time and runs this simplified version:

    my $string = 'three';

    Deparse will show you something cute here:

    $ perl -MO=Deparse -e '$x = (1,2,3)'
    -e syntax OK
    $x = ('???', '???', 3);


    @array[0..1] evaluates to a two element list ("apples", "oranges").
    When this is assigned to a scalar, the result will be "oranges"
    (the last sub-expression)
    But no! All together now: "There is no list."

    I don't think *any* of the builtin operators will return a list
    in scalar context. (Although subroutines can do so.)

    Here the "array slice" operator is aware of the scalar context
    and it (arbitrarily) returns the last element. It could just as
    easily have returned the first element, or the count, or anything
    else.

    I don't mean that it's unpredictable, just that there's no way
    to explain *why* it returns the last element. That's just what
    it does. Perhaps the rationale in this particular case is that
    people are often doing:

    print "The answer is: ", @a[1] + @b[1];

    And they'd run away screaming if they got "2" every time. :-)
    Thanks for this info :-)
    HTH
    It most certainly did
  • Steve Grazzini at Jul 1, 2003 at 10:39 am

    On Tue, Jul 01, 2003 at 11:40:55AM +0200, Kevin Pfeiffer wrote:
    I tried a couple more variations - and wonder why the last
    one doesn't evaluate a count of the slice...
    I think this is interesting:
    $string = @array[0..1];
    print "$string\n";
    # prints "oranges" (why not "2"?)
    Do you expect the slice to return "2" because it looks
    like an array (and that's what an array would do) ?

    Or do you think $string should be "2" because there are
    two elements in the list ?

    --
    Steve
  • Kevin Pfeiffer at Jul 1, 2003 at 2:08 pm

    In article <20030701102954.GA20129@grazzini.net>, Steve Grazzini wrote:
    On Tue, Jul 01, 2003 at 11:40:55AM +0200, Kevin Pfeiffer wrote:
    I tried a couple more variations - and wonder why the last
    one doesn't evaluate a count of the slice...
    I think this is interesting:
    $string = @array[0..1];
    print "$string\n";
    # prints "oranges" (why not "2"?)
    Do you expect the slice to return "2" because it looks
    like an array (and that's what an array would do) ?
    Or do you think $string should be "2" because there are
    two elements in the list ?
    In preparation for a possible answer ("how to admit ignorance in a
    knowledgeable mannner") I went to perldoc and found the question(s), the
    answer(s) and more (thanks, though, to all here who also contributed):

    Found in /usr/lib/perl5/5.8.0/pod/perlfaq4.pod
    What is the difference between a list and an array?

    An array has a changeable length. A list does not. An
    array is something you can push or pop, while a list is a
    set of values. Some people make the distinction that a
    list is a value while an array is a variable. Subroutines
    are passed and return lists, you put things into list con­
    text, you initialize arrays with lists, and you foreach()
    across a list. "@" variables are arrays, anonymous arrays
    are arrays, arrays in scalar context behave like the num­
    ber of elements in them, subroutines access their argu­
    ments through the array @_, and push/pop/shift only work
    on arrays.

    As a side note, there's no such thing as a list in scalar
    context. When you say

    $scalar = (2, 5, 7, 9);

    you're using the comma operator in scalar context, so it
    uses the scalar comma operator. There never was a list
    there at all! This causes the last value to be returned:

    So being a list is a little like being in purgatory - you're neither a
    scalar nor an array (yet)? ;-)


    --
    Kevin Pfeiffer
    International University Bremen
  • Jenda Krynicky at Jul 2, 2003 at 1:09 pm
    Date sent: Tue, 1 Jul 2003 00:56:10 -0700 (PDT)
    From: "Ling F. Zhang" <lingfengz@yahoo.com>
    Send reply to: lingfengz@yahoo.com
    Subject: combining array to scalar
    To: beginners@perl.org
    say I have array:
    @a=["I","LOVE","PERL"]
    I would like to make a scalar:
    $b="I Love Perl\n"

    can I do this with a simple one-liner?
    Assuming you meant

    @a=("I","LOVE","PERL");

    $b = join(' ', map {ucfirst(lc($_))} @a) . "\n";
    print $b;

    Jenda
    ===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery
  • John W. Krahn at Jul 2, 2003 at 5:18 pm

    Jenda Krynicky wrote:

    Assuming you meant

    @a=("I","LOVE","PERL");

    $b = join(' ', map {ucfirst(lc($_))} @a) . "\n";
    $b = qq<@{[map"\u\L$_",@a]}\n>; # :-)

    print $b;

    John
    --
    use Perl;
    program
    fulfillment
  • Jenda Krynicky at Jul 2, 2003 at 6:28 pm
    From: "John W. Krahn" <krahnj@acm.org>
    Jenda Krynicky wrote:
    Assuming you meant

    @a=("I","LOVE","PERL");

    $b = join(' ', map {ucfirst(lc($_))} @a) . "\n";
    $b = qq<@{[map"\u\L$_",@a]}\n>; # :-)
    ($b=lc"@a\n")=~s/\b(.)/\U$1/g;

    or

    ($b="\L@a")=~s/\b(.)/\U$1/g; # :-))

    Jenda
    ===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery
  • Rob Dixon at Jul 2, 2003 at 8:43 pm

    Jenda Krynicky wrote:
    Date sent: Tue, 1 Jul 2003 00:56:10 -0700 (PDT)
    From: "Ling F. Zhang" <lingfengz@yahoo.com>
    Send reply to: lingfengz@yahoo.com
    Subject: combining array to scalar
    To: beginners@perl.org
    say I have array:
    @a=["I","LOVE","PERL"]
    I would like to make a scalar:
    $b="I Love Perl\n"

    can I do this with a simple one-liner?
    Assuming you meant

    @a=("I","LOVE","PERL");

    $b = join(' ', map {ucfirst(lc($_))} @a) . "\n";
    print $b;
    FWIW there's no need for the explicit parameter to 'lc' or
    the parentheses:

    $b = join ' ', map {ucfirst lc} @a;

    Rob

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJul 1, '03 at 7:56a
activeJul 2, '03 at 8:43p
posts14
users8
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase