Pathologically Eclectic Rubbish Lister PerlMonks

### Comment on

 Need Help??

Can you use the scalar range operator with map and grep? Sure, but you have to be careful.

This does what you'd expect:

```grep \$_ == 3 .. \$_ == 7, 1..10    # ==> (3, 4, 5, 6, 7)
[download]```
because grep's first argument is evaluated as a scalar.

This is very puzzling:

```map \$_ == 3 .. \$_ == 7, 1..10    # ==> (0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
[download]```
until you remember that map's first argument is evaluated as a list. What you meant, of course, was:
```map scalar(\$_ == 3 .. \$_ == 7), 1..10    # ==> (,,1,2,3,4,5E0,,,)
[download]```
-- the expected result.

The puzzling case above gets even more puzzling if we show the list that map returns for each input:

```map \$_ == 3 .. \$_ == 7, 1    # ==> (0)
map \$_ == 3 .. \$_ == 7, 2    # ==> (0)
map \$_ == 3 .. \$_ == 7, 3    # ==> ()
map \$_ == 3 .. \$_ == 7, 4    # ==> (0)
map \$_ == 3 .. \$_ == 7, 5    # ==> (0)
map \$_ == 3 .. \$_ == 7, 6    # ==> (0)
map \$_ == 3 .. \$_ == 7, 7    # ==> (0, 1)
map \$_ == 3 .. \$_ == 7, 8    # ==> (0)
map \$_ == 3 .. \$_ == 7, 9    # ==> (0)
map \$_ == 3 .. \$_ == 7, 10   # ==> (0)
[download]```
we get the same ten elements as before, though in an unexpected way. The first line is evaluating:
```1 == 3 .. 1 == 7
[download]```
which is
```"" .. ""
[download]```
Remember that this is being evaluated in list context (because we forgot the scalar()). So, our scalar range operator has accidentally become a list range operator. The final clue is that the list range operator only does string magic (e.g. ("a".."z")) if the strings are non-empty. Otherwise the arguments are converted to integers, in this case zeros:
```0 .. 0
[download]```
which is just a list containing a single zero, as we saw above.

The other cases are now easy to understand.

```map \$_ == 3 .. \$_ == 7, 3
[download]```
is (1 .. ""), which is converted to (1 .. 0), which gives an empty list. Similarly,
```map \$_ == 3 .. \$_ == 7, 7
[download]```
is ("" .. 1) which gives (0, 1).

In reply to Re: The Scalar Range Operator by denishowe
in thread The Scalar Range Operator by pbeckingham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.
• Log In?
 Username: Password:

What's my password?
Create A New User
Chatterbox?
 [shmem]: I'd like to upload a v0.13 [choroba]: Very nice!

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-02-23 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (302 votes). Check out past polls.

Notices?