perl school; scalar/llst behavior in print

Posted on 2006-04-11
Medium Priority
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
  • 2
LVL 85

Expert Comment

ID: 16431584
the . operator works on scalars, so it puts its operands in scalar context.

Author Comment

ID: 16431660
But why 'soup'?   Why not 'pudding'?
LVL 85

Accepted Solution

ozo earned 200 total points
ID: 16431707
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

579 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