What does !! (double exclamation point) mean?

19,150

Solution 1

It is just two ! boolean not operators sitting next to each other.

The reason to use this idiom is to make sure that you receive a 1 or a 0. Actually it returns an empty string which numifys to 0. It's usually only used in numeric, or boolean context though.

You will often see this in Code Golf competitions, because it is shorter than using the ternary ? : operator with 1 and 0 ($test ? 1 : 0).

!! undef  == 0
!! 0      == 0
!! 1      == 1
!! $obj   == 1
!! 100    == 1

undef ? 1 : 0  == 0
0     ? 1 : 0  == 0
1     ? 1 : 0  == 1
$obj  ? 1 : 0  == 1
100   ? 1 : 0  == 1

Solution 2

not-not.

It converts the value to a boolean (or as close as Perl gets to such a thing).

Share:
19,150
Christopher Bottoms
Author by

Christopher Bottoms

I use bash, R, Python, Raku, Perl, Java, and anything else that is needed. Utilizo bash, R, Python, Raku, Perl, Java, o lo que sea necesario. A good explanation of creating good reproducible R questions How to get help in R Python 3 vs Raku Python 3: Unordered common lines between two files: #!/bin/env python import argparse parser = argparse.ArgumentParser(description='Determine the lines in common between two files') parser.add_argument('fileA', help='file name') parser.add_argument('fileB', help='file name') args = parser.parse_args() with open(args.fileA) as x: linesA = x.readlines() with open(args.fileB) as x: linesB = x.readlines() intersection = set(linesA).intersection(set(linesB)) print("".join(sorted(intersection))) Usage message resulting from python intersection_of_lines.py --help: usage: intersection_of_lines.py [-h] fileA fileB Determine the lines in common between two files positional arguments: fileA file name fileB file name optional arguments: -h, --help show this help message and exit Raku: Unordered common lines between two files: #!/bin/env raku sub MAIN #= Determine the lines in common between two files ( $fileA, #= file name $fileB, #= file name ) { my @linesA = $fileA.IO.lines; my @linesB = $fileB.IO.lines; my $intersection = @linesA ∩ @linesB; put $intersection.keys.sort.join("\n"); } Yes that is ∩, a real Unicode intersection symbol. The ASCII version (&) also works. You also get a nice usage message resulting from raku intersection_of_lines.raku --help: Usage: ./intersection_of_lines.raku <fileA> <fileB> -- Determine the lines in common between two files <fileA> file name <fileB> file name Raku tidbits Raku is a very clean, concise, understandable language. Its design pulls from the strengths of other languages. Raku's rotor (the "king of list manipulation"). Error handling example. SLURM Pass variables to SLURM: Passing SLURM batch command line arguments to R sbatch --export=GREETING=Hello,PLACE=world --wrap='echo "$GREETING $PLACE" > howdy.txt' convert Jupyter notebook (https://stackoverflow.com/a/50567584/215487) jupyter-nbconvert --to python notebook.ipynb --stdout --TemplateExporter.exclude_input_prompt=True Why adjusted P-values are so important: https://www.xkcd.com/882

Updated on June 04, 2022

Comments

  • Christopher Bottoms
    Christopher Bottoms almost 2 years

    In the code below, from a blog post by Alias, I noticed the use of the double exclamation point !!. I was wondering what it meant and where I could go in the future to find explanations for Perl syntax like this. (Yes, I already searched for !! at perlsyn).

    package Foo;
    
    use vars qw{$DEBUG};
    BEGIN {
        $DEBUG = 0 unless defined $DEBUG;
    }
    use constant DEBUG => !! $DEBUG;
    
    sub foo {
        debug('In sub foo') if DEBUG;
    
        ...
    }
    

    UPDATE
    Thanks for all of your answers.

    Here is something else I just found that is related The List Squash Operator x!!

  • mpeters
    mpeters over 14 years
    It's also a common idiom in other languages too like Javascript.
  • Ether
    Ether over 14 years
    I actually often use !!!! in my code, because !! confuses vim's syntax highlighting and another !! will restore it!
  • Christopher Bottoms
    Christopher Bottoms over 14 years
    @David Dorward Thanks for your answer!
  • Paul Nathan
    Paul Nathan over 14 years
    @Ether: Obviously, you need to use emacs. :)
  • cjm
    cjm over 14 years
    This is not correct. When an operator has to generate a false value, it uses a magic value that evaluates as the empty string in string context or 0 in numeric context. (The exceptions are operators like and and or, which simply return the operand that caused them to return false.) (See perldoc.perl.org/perlsyn.html#Truth-and-Falsehood)
  • ysth
    ysth over 14 years
    It produces 1 or the canonical false value, not 0. This is 0 in numeric context but the empty string in string context. To get 0 or 1, use the 1-! operator :)
  • hobbs
    hobbs over 14 years
    undef is also a magic value that evaluates as '' in string context and 0 in numeric context -- the only difference is that when warnings are enabled, using undef for its string or numeric value is apt to produce a warning, while 'the false value' does no such thing ;)
  • Ankit Roy
    Ankit Roy over 14 years
    @Brad: -1 to get your attention on ysth's comment, will revoke upon fix.
  • cjm
    cjm over 14 years
    @hobbs: Well, another difference is that defined(undef) is false, but defined(1==2) is true.
  • Adam Kennedy
    Adam Kennedy about 14 years
    Speaking as the guy that wrote the same code, it's there because I want anything odd in that variable normalised away at compile time. It's just a sort of quality protection mechanism.
  • Jacob
    Jacob about 14 years
    Actually it returns a zero length string.
  • Christopher Bottoms
    Christopher Bottoms about 14 years
    +1 You obviously had an influence on the other answers. Brad modified his answer after yours. Also, at least one of those you mentioned ended up deleting his or her answer as well.
  • Christopher Bottoms
    Christopher Bottoms almost 14 years
    @Brad Thanks for your answer. I just started reading a draft of chromatic's new book and I like how he says that !! "forces boelean context".
  • Jacob
    Jacob over 11 years
    @molecules Actually I modified my answer based on the comment from ysth.