Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it printing all three printing statements?

Tags:

perl

my %hash = ( 0=> , 1=>"Man");
my $key=0;
print "Exists\n"    if exists $hash{$key};
print "Defined\n"   if defined $hash{$key};
print "True\n"      if $hash{$key};

Why is the above Perl code printing all three print statements?

It should print only Exists, shouldn't it?

like image 391
Man Avatar asked May 16 '11 07:05

Man


People also ask

What is the purpose of the print statement?

Use the PRINT statement to send data to the screen, a line printer, or another print file. The ON clause specifies the logical print channel to use for output.

How do you print two statements in python?

To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3.

How do I stop the next line from printing in Python?

To print without a new line in Python 3 add an extra argument to your print function telling the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') The next print function will be on the same line.


3 Answers

use strict; use warnings;. Always.

Your hash declaration is not doing what you think it's doing, it has an odd number of elements.

Try this:

use Data::Dumper;
my %hash = ( 0=> , 1=>"Man");
print Dumper(%hash);

You'll see that $hash{0} is set to 1, $hash{"Man"} exists but is undef, and $hash{1} does not exist at all. i.e. your hash declaration is equivalent to:

my %hash = (0 => 1, "Man" => undef);

Why is this happening? It's because:

  • => is essentially an equivalent of ,
  • List value constructors work that way, e.g. ($a,,$b) is equivalent to ($a,$b)

    Relevant quotes from that document:

    The => operator is mostly just a more visually distinctive synonym for a comma, but it also arranges for its left-hand operand to be interpreted as a string if it's a bareword that would be a legal simple identifier.

    And:

    The null list is represented by (). Interpolating it in a list has no effect. Thus ((),(),()) is equivalent to (). Similarly, interpolating an array with no elements is the same as if no array had been interpolated at that point.

    (...)

    The list 1,,3 is a concatenation of two lists, 1, and 3, the first of which ends with that optional comma. 1,,3 is (1,),(3) is 1,3 (And similarly for 1,,,3 is (1,),(,),3 is 1,3 and so on.) Not that we'd advise you to use this obfuscation.

    Apply this to your code:

       (0 => , 1 => "Man");
    is (0 , , 1 , "Man");
    is (0 , 1 , "Man");
    
  • like image 50
    Mat Avatar answered Oct 12 '22 22:10

    Mat


    Always, always, ALWAYS use strict; and use warnings; in your code:

    use strict;
    use warnings;
    
    my %hash = ( 0=> , 1=>"Man");
    my $key=0;
    print "Exists\n"    if exists $hash{$key};
    print "Defined\n"   if defined $hash{$key};
    print "True\n"      if $hash{$key};
    

    Output:

    Odd number of elements in hash assignment at - line 3.
    

    If you want an element to exist but not be defined, use undef:

    my %hash = ( 0=> undef, 1=>"Man");
    
    like image 32
    Chris Lutz Avatar answered Oct 13 '22 00:10

    Chris Lutz


    use warnings; and you'll see Odd number of elements in hash assignment.

    That's it! You've got (0=>1, "Man"=>undef).

    like image 21
    Dallaylaen Avatar answered Oct 12 '22 22:10

    Dallaylaen