Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

I was expecting 4 elements in the array, but it only has 3

by ricardo_sdl (Novice)
on Nov 28, 2018 at 16:48 UTC ( #1226460=perlquestion: print w/replies, xml ) Need Help??

ricardo_sdl has asked for the wisdom of the Perl Monks concerning the following question:

This is the code:

use 5.010; use strict; use warnings; use Data::Dumper; use Config; my @list = ("housekeeper" =~ /keeper/, "housekeeper" =~ /^keeper/, "housekeeper" =~ /keeper$/, "housekeeper\n" =~ /keeper$/); say "list"; say scalar(@list); print Dumper(@list); foreach(@list) { say; } my @other_list = (1 == 1, 1 == 0, 0 == 0, 1 != 0); say "other_list"; say scalar(@other_list); print Dumper(@other_list); foreach(@other_list) { say; }

I was expecting 4 "boolean" values in the @list array. But apparently it only has 3 elements, all truthy ones. The @other_list has 4 elements as I was expecting. What am I missing here?

I've got the same results on windows (strawberry-perl- and linux (5.018002)


Replies are listed 'Best First'.
Re: I was expecting 4 elements in the array, but it only has 3
by Paladin (Vicar) on Nov 28, 2018 at 17:08 UTC

    The difference is context, scalar vs. list.

    The m// operator returns the empty list on no match in list context and the list (1) in list context when there are no () in the regex.

    So after evaluating all the matches in @list you are left with ((1), (), (1), (1)), which evaluates to (1,1,1).

Re: I was expecting 4 elements in the array, but it only has 3
by j41r (Beadle) on Nov 30, 2018 at 00:18 UTC

    According to List value constructors:

    when a LIST is evaluated, each element of the list is evaluated in list context, and the resulting list value is interpolated into LIST just as if each individual element were a member of LIST.

    It also says:

    The null list is represented by (). Interpolating it in a list has no effect. Thus ((),(),()) is equivalent to ()

    Paladin and haukex have already explained why m// is returning an empty list, but you can also check it by yourself in Regexp Quote Like Operators:

    If the /g option is not used, m// in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, that is, ($1, $2, $3...) (Note that here $1 etc. are also set). When there are no parentheses in the pattern, the return value is the list (1) for success. With or without parentheses, an empty list is returned upon failure.
      On the other hand ...

      But nothing in the second quote from the docs is "on the other hand" or, as I would interpret the phrase, "contrary" to the first quote. Did you mean to suggest otherwise?

      Give a man a fish:  <%-{-{-{-<

        Oh, thank you AnomalousMonk for your suggestion. You’re right. Do you think the phrase "in addition" would be appropriate here, instead of "on the other hand"?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1226460]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2022-07-04 06:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found