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

I can do this:
print "data is " , $mac eq $old ? "SAME" : "DIFF" , "\n";
But now I just want to do this:
$mac eq $old ? "SAME" : "DIFF" , "\n";
but it doesnt work. What am I doing wrong?

Replies are listed 'Best First'.
Re: conditions not working
by davido (Cardinal) on Sep 29, 2003 at 19:43 UTC
    When you say "doesn't work" are you referring to the warning "Useless use of a constant in void context in line xxx"? Or did you neglect to "use warnings;" and thus are unaware of the fact that your second example isn't doing anything with the results of the trinary evaluation, nor with the results of the comma operator?

    You're evaluating a conditional (trinary operator) but doing nothing with it, in the second example. It's useless to use it in void context. Also, your use of a "\n" after the comma operator is a useless use in this context.

    Perhaps you mean something like:

    $mac = ($mac eq $old ? "SAME" : "DIFF")."\n";

    But that really is only a guess at your intentions. Perl just chose not to play the guessing game. ;)

    Always use strict and use warnings to catch common pitfalls.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: conditions not working
by waswas-fng (Curate) on Sep 29, 2003 at 19:41 UTC
    You can simplify the the first attempt (assuming $mac and $old are the same) as:
    print "data is SAME\n";
    And the second as:
    "SAME", "\n";

    Does that make it clear to you what is wrong?


    -Waswas
Re: conditions not working
by vbrtrmn (Pilgrim) on Sep 29, 2003 at 20:49 UTC
    One line...
    print "Data is ".($mac eq $old ? "SAME" : "DIFF")."\n";

    --
    paul

Re: conditions not working
by welchavw (Pilgrim) on Sep 29, 2003 at 19:39 UTC

    I assume you are trying to assign???

    $mac = $old = 1; #$mac--; $mac = $mac eq $old ? "SAME" : "DIFF" , "\n"; print "$mac\n";

    Otherwise, you have not provided enough context.

    ,welchavw

      Did you test your solution with "use warnings;"?

      You still have a useless use of a constant in void context. (Hint: comma operator)

      Dave

      "If I had my life to do over again, I'd be a plumber." -- Albert Einstein