perl school; scalar/llst behavior in print

Posted on 2006-04-11
Last Modified: 2010-03-05

%day = (
  Mon => "pudding" ,
  Tue => "soup",
  Wed => "carrots",
  Thr => "beans",
  Fri => "fish"
What's the rule about list verse scalar context that
has the second print only printing the last value in a 'list'?

  print @day{'Mon', 'Tue'};         # print 'puddingsoup'

  print @day{'Mon', 'Tue'} . "\n"  # prints 'soup\n'
Question by:hank1
    LVL 84

    Expert Comment

    the . operator works on scalars, so it puts its operands in scalar context.
    LVL 1

    Author Comment

    But why 'soup'?   Why not 'pudding'?
    LVL 84

    Accepted Solution

    perldoc perldata
           In a context not requiring a list value, the value of what appears to
           be a list literal is simply the value of the final element, as with the
           C comma operator.  For example,

               @foo = ('cc', '-E', $bar);

           assigns the entire list value to array @foo, but

               $foo = ('cc', '-E', $bar);

           assigns the value of variable $bar to the scalar variable $foo.  Note
           that the value of an actual array in scalar context is the length of
           the array; the following assigns the value 3 to $foo:

               @foo = ('cc', '-E', $bar);
               $foo = @foo;                # $foo gets 3

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now