comma operator - Page 3

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: comma operator

Franken Sense wrote:
Quoted text here. Click to load it

in perl, whether ("Sun", "Sunday") is recognized as a list containing
two string elements separated by a comma or as a parenthesized comma
expression with a value of "Sunday" depends upon whether it occurs in
a list context or a scalar context. Since this is a list context, none
of those commas are comma operators. Try the following, where all of
the commas are comma operators:

     my $longday2 =
        ("Sun" , "Sunday"),
        ("Mon" ,"Monday"),
        ("Tue" ,"Tuesday"),

You will find that $longday2 has a value of "Tuesday".

The definition of %longday1 given above is syntactically comparable
(the semantics are quite different) to the C definition:

    char *longday3[][2] =
        {"Sun" , "Sunday"},
        {"Mon" ,"Monday"},
        {"Tue" ,"Tuesday"},

where the commas are also not comma operators. In C, the fact that
commas separating elements of an initializer list are not comma
operators is indicated by the fact that the grammar requires that
elements of initializer lists must be assignment-expressions. The
result of applying the comma operator is an expression, but it doesn't
qualify as an assignment-expression.

If you parenthesize an expression, it becomes a primary expression,
which does qualify as as an assignment expression. It would,
therefore, be perfectly possible to have comma operator in an
initializer, but only if it is parenthesized. Three of the six commas
in the following declaration are comma operators:

    char *longday4[] =
        ("Sun" , "Sunday"),
        ("Mon" ,"Monday"),
        ("Tue" ,"Tuesday"),

Re: comma operator

jameskuyper wrote:
Quoted text here. Click to load it

That was incorrectly stated. As Keith correctly indicated, perl does
indeed refer to those as "comma operators", both in a list context and
in a scalar context, though the operation they perform is quite
different in those two cases. Only the scalar context operation
corresponds to what C calls a "comma operator".

Re: comma operator


Quoted text here. Click to load it

I take it you meant "the *comma* operator in C". Just in case you
there is no "comment operator" in C


Site Timeline