Undefined values
You are encouraged to solve this task according to the task description, using any language you may know.
For languages which have an explicit notion of an undefined value, identify and exercise those language's mechanisms for identifying and manipulating a variable's value's status as being undefined
Perl
<lang perl>#!/usr/bin/perl -w use strict;
- Declare the variable
our $var;
- Check to see whether it is defined
print "var contains an undefined value at first check\n" unless defined $var;
- Give it a value
$var = "Chocolate";
- Check to see whether it is defined after we gave it the
- value "Chocolate"
print "var contains an undefined value at second check\n" unless defined $var;
- Give the variable an undefined value.
$var = undef;
- or, equivalently:
undef $var;
- Check to see whether it is defined after we've explicitly
- given it an undefined value.
print "var contains an undefined value at third check\n" unless defined $var;
- Give the variable a value of 42
$var = 42;
- Check to see whether the it is defined after we've given it
- the value 42.
print "var contains an undefined value at fourth check\n" unless defined $var;
- Because most of the output is conditional, this serves as
- a clear indicator that the program has run to completion.
print "Done\n";</lang>
Results in:
var contains an undefined value at first check var contains an undefined value at third check Done
PHP
<lang php><?php // Check to see whether it is defined if (!isset($var))
echo "var is undefined at first check\n";
// Give it a value $var = "Chocolate";
// Check to see whether it is defined after we gave it the // value "Chocolate" if (!isset($var))
echo "var is undefined at second check\n";
// Give the variable an undefined value. unset($var);
// Check to see whether it is defined after we've explicitly // given it an undefined value. if (!isset($var))
echo "var is undefined at third check\n";
// Give the variable a value of 42 $var = 42;
// Check to see whether the it is defined after we've given it // the value 42. if (!isset($var))
echo "var is undefined at fourth check\n";
// Because most of the output is conditional, this serves as // a clear indicator that the program has run to completion. echo "Done\n"; ?></lang>
Results in:
var is undefined at first check var is undefined at third check Done
Python
<lang python>
- Check to see whether it is defined
try: var except NameError: print "var is undefined at first check"
- Give it a value
var = "Chocolate"
- Check to see whether it is defined after we gave it the
- value "Chocolate"
try: var except NameError: print "var is undefined at second check"
- Give the variable an undefined value.
del var
- Check to see whether it is defined after we've explicitly
- given it an undefined value.
try: var except NameError: print "var is undefined at third check"
- Give the variable a value of 42
var = 42
- Check to see whether the it is defined after we've given it
- the value 42.
try: var except NameError: print "var is undefined at fourth check"
- Because most of the output is conditional, this serves as
- a clear indicator that the program has run to completion.
print "Done" </lang>
Results in:
var is undefined at first check var is undefined at third check Done
Ruby
<lang ruby>
- Check to see whether it is defined
puts "var is undefined at first check" unless defined? var
- Give it a value
var = "Chocolate"
- Check to see whether it is defined after we gave it the
- value "Chocolate"
puts "var is undefined at second check" unless defined? var
- I don't know any way of undefining a variable in Ruby
- Because most of the output is conditional, this serves as
- a clear indicator that the program has run to completion.
puts "Done" </lang>
Results in:
var is undefined at first check Done