Convert array into list context

Discussion in 'Perl Misc' started by Ryan Chan, Jun 13, 2010.

  1. Ryan Chan

    Ryan Chan Guest

    Consider the simple code.

    ============
    my @a = (1,2,'c');

    my $s = ('a' , @a);
    print $s;
    ============


    How to print out 'c' instead of 3?
     
    Ryan Chan, Jun 13, 2010
    #1
    1. Advertising

  2. Ryan Chan

    Alan Curry Guest

    Ryan Chan <> wrote:
    >Consider the simple code.
    >
    >============
    >my @a = (1,2,'c');
    >
    >my $s = ('a' , @a);
    >print $s;
    >============
    >
    >
    >How to print out 'c' instead of 3?
    >


    Oh that wacky scalar-context comma operator! You were hoping it would
    flatten the list and then act as if the elements of @a had been
    separated by scalar-context comma operators, yielding the last element
    of the flattened list? Too clever.

    my $s = ('a' , @a)[-1];

    In addition to actually working, the [-1] index also makes it obvious to
    the later maintenance programmer that you wanted the last element of the
    list. Explicit is better than implicit...

    --
    Alan Curry
     
    Alan Curry, Jun 13, 2010
    #2
    1. Advertising

  3. Ryan Chan

    Ryan Chan Guest

    Hi,

    On 6月13æ—¥, 下åˆ5時53分, (Alan Curry) wrote:
    > my $s = ('a' , @a)[-1];
    >



    Yes, this work. Thanks.

    But it is ambiguous that we don't know when the array is being
    flattened, e.g. why not @a is being taken out.
     
    Ryan Chan, Jun 13, 2010
    #3
  4. Ryan Chan

    Guest

    On Sun, 13 Jun 2010 01:42:54 -0700 (PDT), Ryan Chan <> wrote:

    >Consider the simple code.
    >
    >============
    >my @a = (1,2,'c');
    >
    >my $s = ('a' , @a);
    >print $s;
    >============
    >
    >
    >How to print out 'c' instead of 3?


    @a is viewed in scalar context giving the # of elements.
    You can expand the RHS a couple of ways.

    With the Array slice notation on the list element -
    $s = ('a' , @a[0..$#a]);

    or index the list where [-1] gets last element of the expanded list -
    $s = ('a' , @a)[-1]; # best way

    or, a little of both -
    $s = ('a' , @a[0..$#a])[-1]; # redundant

    -sln
     
    , Jun 13, 2010
    #4
  5. Ryan Chan

    Uri Guttman Guest

    >>>>> "RC" == Ryan Chan <> writes:

    RC> Consider the simple code.
    RC> ============
    RC> my @a = (1,2,'c');

    RC> my $s = ('a' , @a);

    regardless, that is throwing away 'a'. if you had warnings enabled, that
    would be flagged. so why are you doing that? there is no benefit to that
    construct.

    RC> print $s;
    RC> ============

    RC> How to print out 'c' instead of 3?

    why are you trying to do it the wrong and hard way? arrays in scalar
    context return their count.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Jun 13, 2010
    #5
  6. Uri Guttman wrote:
    >>>>>> "RC" == Ryan Chan <> writes:

    >
    > RC> Consider the simple code.
    > RC> ============
    > RC> my @a = (1,2,'c');
    >
    > RC> my $s = ('a' , @a);
    >
    > regardless, that is throwing away 'a'. if you had warnings enabled, that
    > would be flagged. so why are you doing that? there is no benefit to that
    > construct.


    Of course not. But it did what he wanted, then there would be.

    Xho
     
    Xho Jingleheimerschmidt, Jun 13, 2010
    #6
  7. On 2010-06-13, Ryan Chan <> wrote:
    >> my $s = ('a' , @a)[-1];


    > Yes, this work. Thanks.


    > But it is ambiguous that we don't know when the array is being
    > flattened, e.g. why not @a is being taken out.


    The only thing ambiguous is who is "we" in this sentence. Scalar vs
    List context is on of the few parts of Perl which has pretty good
    documentation.

    Yours,
    Ilya
     
    Ilya Zakharevich, Jun 14, 2010
    #7
  8. Ryan Chan

    Uri Guttman Guest

    >>>>> "XJ" == Xho Jingleheimerschmidt <> writes:

    XJ> Uri Guttman wrote:
    >>>>>>> "RC" == Ryan Chan <> writes:

    >>

    RC> Consider the simple code.
    RC> ============
    RC> my @a = (1,2,'c');
    >>

    RC> my $s = ('a' , @a);
    >>
    >> regardless, that is throwing away 'a'. if you had warnings enabled, that
    >> would be flagged. so why are you doing that? there is no benefit to that
    >> construct.


    XJ> Of course not. But it did what he wanted, then there would be.

    me thinks you have a grammar mistake or i don't get your second sentence
    at all.

    and the OP's need is still silly.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Jun 14, 2010
    #8
  9. Uri Guttman wrote:
    >>>>>> "XJ" == Xho Jingleheimerschmidt <> writes:

    >
    > XJ> Uri Guttman wrote:
    > >>>>>>> "RC" == Ryan Chan <> writes:
    > >>

    > RC> Consider the simple code.
    > RC> ============
    > RC> my @a = (1,2,'c');
    > >>

    > RC> my $s = ('a' , @a);
    > >>
    > >> regardless, that is throwing away 'a'. if you had warnings enabled, that
    > >> would be flagged. so why are you doing that? there is no benefit to that
    > >> construct.

    >
    > XJ> Of course not. But it did what he wanted, then there would be.
    >
    > me thinks you have a grammar mistake or i don't get your second sentence
    > at all.


    It was supposed to be "But if it did".


    > and the OP's need is still silly.


    Make $s be the last thing in @a, or 'a' if @a is empty. Of course, the
    ()[-1] construct handles that nicely. I'm not sure why that need is silly.

    Xho
     
    Xho Jingleheimerschmidt, Jun 14, 2010
    #9
  10. Ryan Chan

    Uri Guttman Guest

    >>>>> "XJ" == Xho Jingleheimerschmidt <> writes:

    XJ> Uri Guttman wrote:
    >>>>>>> "XJ" == Xho Jingleheimerschmidt <> writes:

    >>

    XJ> Of course not. But it did what he wanted, then there would be.
    >>
    >> me thinks you have a grammar mistake or i don't get your second sentence
    >> at all.


    XJ> It was supposed to be "But if it did".


    >> and the OP's need is still silly.


    XJ> Make $s be the last thing in @a, or 'a' if @a is empty. Of course,
    XJ> the ()[-1] construct handles that nicely. I'm not sure why that need
    XJ> is silly.

    the need for getting the last element of a list with a fixed element
    that is lost is silly. yes it could be done with ()[-1] but not the way
    it was coded nor did he say that was the goal (defaulting to 'a'). i
    would code it this way which is clear that is the goal:

    $x = $a[0] || 'a' ;

    of course change that to -1 if you want the last element and use // if
    you want it to work with 0 vs defined. this way makes it clear that 'a'
    is a default and also it doesn't build up a wasteful list.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Jun 14, 2010
    #10
  11. Ryan Chan <> wrote:
    >But it is ambiguous that we don't know when the array is being
    >flattened,


    I may very well be mistaken but I tend to believe that flattening
    happens only for function arguments and return values.

    jue
     
    Jürgen Exner, Jun 14, 2010
    #11
  12. Ryan Chan

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    JE> Ryan Chan <> wrote:
    >> But it is ambiguous that we don't know when the array is being
    >> flattened,


    JE> I may very well be mistaken but I tend to believe that flattening
    JE> happens only for function arguments and return values.

    it depends on the function (builtin or sub). some builtins have
    prototypes where an array is passed whole (e.g. splice, shift) as scalar
    args may follow which are kept separate and not slurped in. and you can
    code subs with prototypes like that as well. i generally stay away from
    sub protos but recognize they exist in builtins.

    actually all list contexts will flatten an array (or multiple
    arrays). most are provided by functions or return but you missed
    assignment (and return's context is provided by what it is assigned
    to).

    also indexing provides list context:

    $x = (1, 4, @a) ;
    $x = (1, 4, @a)[$y] ;

    the first line was the example the OP posted.

    there are likely others but i can't think clearly now. been a long weekend.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Jun 14, 2010
    #12
    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. Juergen Berchtel
    Replies:
    1
    Views:
    6,008
    John C. Bollinger
    May 20, 2005
  2. Klaus Neuner
    Replies:
    7
    Views:
    498
    Klaus Neuner
    Jul 26, 2004
  3. Royan
    Replies:
    32
    Views:
    995
  4. noydb
    Replies:
    12
    Views:
    344
    noydb
    Aug 19, 2011
  5. Bo Lindbergh

    List context versus list context

    Bo Lindbergh, Jun 21, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    229
    Charles DeRykus
    Jun 28, 2006
Loading...

Share This Page