My understanding for a long time was that basically never to use ==, since it doesn't respect type and can end up causing undesired results.  I just read this post about the general comparison   but wasn't sure if everyone has the same opinion as me.   Here is the scenario which I encountered:  Original code  $state = (intval($state) === TIMESHEET_STATE_UNLOCKED) ? 'unlock' : 'lock';   We had found that this line was invalid, since our constant TIMESHEET_STATE_UNLOCKED was a boolean and $state was the string '0' or '1'. We could fix this in one of two ways:  $state = (intval($state) === intval(TIMESHEET_STATE_UNLOCKED)) ? 'unlock' : 'lock';   OR  $state = ($state == intval(TIMESHEET_STATE_UNLOCKED)) ? 'unlock' : 'lock';   My first inclination was to former but I'm not so sure anymore.
7905
What is the difference between == and ===?  How exactly does the loosely == comparison work? How exactly does the strict === comparison work?  What would be some useful examples?
9792
Yesterday I had a two-hour technical phone interview (which I passed, woohoo!), but I completely muffed up the following question regarding dynamic binding in Java.  And it's doubly puzzling because I used to teach this concept to undergraduates when I was a TA a few years ago, so the prospect that I gave them misinformation is a little disturbing...  Here's the problem I was given:  /* What is the output of the following program? */  public class Test {    public boolean equals( Test other ) {     System.out.println( "Inside of Test.equals" );     return false;   }    public static void main( String [] args ) {     Object t1 = new Test();     Object t2 = new Test();     Test t3 = new Test();     Object o1 = new Object();      int count = 0;     System.out.println( count++ );// prints 0     t1.equals( t2 ) ;     System.out.println( count++ );// prints 1     t1.equals( t3 );     System.out.println( count++ );// prints 2     t3.equals( o1 );     System.out.println( count++ );// prints 3     t3.equals(t3);     System.out.println( count++ );// prints 4     t3.equals(t2);   } }   I asserted that the output should have been two separate print statements from within the overridden equals() method: at t1.equals(t3) and t3.equals(t3).  The latter case is obvious enough, and with the former case, even though t1 has a reference of type Object, it is instantiated as type Test, so dynamic binding should call the overridden form of the method.  Apparently not.  My interviewer encouraged me to run the program myself, and lo and behold, there was only a single output from the overridden method: at the line t3.equals(t3).  My question then is, why?  As I mentioned already, even though t1 is a reference of type Object (so static binding would invoke Object's equals() method), dynamic binding should take care of invoking the most specific version of the method based on the instantiated type of the reference.  What am I missing?
43371
PHP has the habit of evaluating (int)0 and (string)"0" as empty when using the empty() function. This can have unintended results if you expect numerical or string values of 0. How can I "fix" it to only return true to empty objects, arrays, strings, etc?
16336
Given the following code (that doesn't work):  while True:     #snip: print out current state     while True:         ok = get_input("Is this ok? (y/n)")         if ok.lower() == "y": break 2 #this doesn't work :(         if ok.lower() == "n": break     #do more processing with menus and stuff   Is there a way to make this work? Or do I have do one check to break out of the input loop, then another, more limited, check in the outside loop to break out all together if the user is satisfied?
80225
Evidently hash keys are compared in a case-sensitive manner.  $ perl -e '%hash = ( FOO =&gt; 1 ); printf "%s\n", ( exists $hash{foo} ) ? "Yes" : "No";' No  $ perl -e '%hash = ( FOO =&gt; 1 ); printf "%s\n", ( exists $hash{FOO} ) ? "Yes" : "No";' Yes   Is there a setting to change that for the current script?
28013
Please consider the following MWE - where I try to test for string equality between an parameter, and a test string prepared by a macro:  \documentclass{article}  \def\testStr#1{IOIO#1}  \newcommand{\doTest}[1]{% \edef\tmphwp{\testStr{XX}} % \typeout{=\tmphwp=#1=} % \ifx\tmphwp#1{% \typeout{~equal}% }\else{% \typeout{!not equal}% }\fi % \typeout{ ^^J } % newline in terminal stdout }  \begin{document}  \doTest{12}  \doTest{hello}  \doTest{IOIOXX}  \end{document}   Strangely, this seems to always determine strings are unequal - even if I enter specifically the right string - terminal log (from pdflatex) reports:   =IOIOXX=12= !not equal   =IOIOXX=hello= !not equal   =IOIOXX=IOIOXX= !not equal   Hmm ... I would have expected IOIOXX = IOIOXX ?!   Can anyone explain why am I not getting the expected results - and how to get the right comparison? Does this have maybe to do anything with catcodes?  Many thanks in advance for any answers, Cheers!
81587
I've seen several examples of code like this:  if not someobj:     #do something   But I'm wondering why not doing:  if someobj == None:     #do something   Is there any difference? Does one have an advantage over the other?
79565
Are there any differences between...  if ($value) {  }   ...and...  if ($value):  endif;   ?
4564
I can write  VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}"   the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
112023
In PHP, I can write:  $vname = 'phone'; $$vname = '555-1234'; print $phone;   ... And the script will output "555-1234".  Is there any equivalent in Perl?  Is there any way to constrain $phone to the scope of the local block, as if I'd written my $phone? Using my $$vname gives me "Can't declare scalar dereference in my at ..." errors.
72634
I have this legacy code that I am working with and there is code like this all over the place:       #If PRE611 = True Then         'Do Something     #Else         'Something Else     #End If   I am pretty sure the PRE611 has something to do with versioning, but I would like to know what specifically the # sign does.
2830
I want to check if a variable has a valid year using a regular expression. Reading the  I understand I could use the operator =~  Looking at the example below, I would expect to see "not OK" but I see "OK". What am I doing wrong?  i="test" if [ $i=~"200[78]" ] then   echo "OK" else   echo "not OK" fi
63865
Since arrays and hashes can only contain scalars in Perl, why do you have to use the $ to tell the interpreter that the value is a scalar when accessing array or hash elements? In other words, assuming you have an array @myarray and a hash %myhash, why do you need to do:  $x = $myarray[1]; $y = $myhash{'foo'};   instead of just doing :  $x = myarray[1]; $y = myhash{'foo'};   Why are the above ambiguous?   Wouldn't it be illegal Perl code if it was anything but a $ in that place? For example, aren't all of the following illegal in Perl?  @var[0]; @var{'key'}; %var[0]; %var{'key'};
44106
Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I'm not asking for code that can handle both cases; they will be handled in separate functions.  Obviously, I can do something like:  double clampedA; double a = calculate(); clampedA = a &gt; MY_MAX ? MY_MAX : a; clampedA = a &lt; MY_MIN ? MY_MIN : a;   or  double a = calculate(); double clampedA = a; if(clampedA &gt; MY_MAX)     clampedA = MY_MAX; else if(clampedA &lt; MY_MIN)     clampedA = MY_MIN;   The fixpoint version would use functions/macros for comparisons.  This is done in a performance-critical part of the code, so I'm looking for an as efficient way to do it as possible (which I suspect would involve bit-manipulation)   EDIT: It has to be standard/portable C, platform-specific functionality is not of any interest here. Also, MY_MIN and MY_MAX are the same type as the value I want clamped (doubles in the examples above).
37655
In Bash, two integers can be compared using conditional expression     arg1 OP arg2      OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These   arithmetic binary operators return true if arg1 is equal to, not equal   to, less than, less than or equal to, greater than, or greater than or   equal to arg2, respectively. Arg1 and arg2 may be positive or negative   integers.   or arithmetic expression:     &lt;= &gt;= &lt; &gt; comparison      == != equality and inequality   Why do we have two different ways for comparing two integers? When to use which?  For example, [[ 3 -lt 2 ]] uses conditional expression, and (( 3 &lt; 2 )) uses arithmetic expression. Both returns 0 when the comparison is true  When comparing two integers, can these two methods always be used interchangeably? If yes, why does Bash have two methods rather than one?
4066
Are the following two boolean expressions the same?  if [ -n $1 ] ; then  if [ -n "$1" ] ; then   If not - When should you put a variable in quotes?
30702
Is there a way of using an 'OR' operator or equivalent in a PHP switch?  For example, something like this:  switch ($value) {      case 1 || 2:         echo 'the value is either 1 or 2';         break; }