[TT #592][docs] Clarify docs about the case sensitivity of the debugger
[parrot.git] / examples / tutorial / 57_exists.pir
blob5fa0760b9173d7aef02de2e81c15759fd147cf40
1 # Copyright (C) 2007-2009, Parrot Foundation.
2 # $Id$
4 =head1 exists
6 The C<exists> opcode tells you whether an element of a container PMC
7 (array or hash for example) exists. It differs from C<defined>, because
8 C<defined> will return false on undefined values, which are valid entries.
10 It's important to think of an array or hash as being a collection of data
11 buckets. That bucket can exist but not contain a defined value. If the
12 bucket does not exist, there is no value to check.
14 In the example below, C<my_array[0]> exists and is defined, but
15 C<my_array[1]> only exists without ever being given a defined value.
16 C<my_array[2]> does not exist and is thus not defined. Similar to
17 C<defined>, the behavior of C<exists> for a given PMC depends on how
18 that PMC implements the related vtable functions.
20 =cut
22 .sub main :main
24     # set up an array with two elements
25     .local pmc my_array
26     my_array = new ['ResizablePMCArray']
27     $P0      = new ['String']
28     $P0      = "Hello"
29     $P1      = new ['Undef']
30     push my_array, $P0
31     push my_array, $P1
33     # looking at the first element
34     $I1 = defined my_array[0]
35     unless $I1 goto not_def_0
36         say "my_array[0] is defined"
37         goto end_check_defined_0
38     not_def_0:
39         say "my_array[0] is not defined"
40     end_check_defined_0:
42     $I2 = exists my_array[0]
43     unless $I2 goto not_exists_0
44         say "my_array[0] exists"
45         goto end_check_exists_0
46     not_exists_0:
47         say "my_array[1] does not exist"
48     end_check_exists_0:
51     # looking at the second element
52     $I3 = defined my_array[1]
53     unless $I3 goto not_def_1
54         say "my_array[1] is defined"
55         goto end_check_defined_1
56     not_def_1:
57         say "my_array[1] is not defined"
58     end_check_defined_1:
60     $I4 = exists my_array[1]
61     unless $I4 goto not_exists_1
62         say "my_array[1] exists"
63         goto end_check_exists_1
64     not_exists_1:
65         say "my_array[1] does not exist"
66     end_check_exists_1:
69     # looking at the third element
70     $I5 = defined my_array[2]
71     unless $I5 goto not_def_2
72         say "my_array[2] is defined"
73         goto end_check_defined_2
74     not_def_2:
75         say "my_array[2] is not defined"
76     end_check_defined_2:
78     $I6 = exists my_array[2]
79     unless $I6 goto not_exists_2
80         say "my_array[2] exists"
81         goto end_check_exists_2
82     not_exists_2:
83         say "my_array[2] does not exist"
84     end_check_exists_2:
86 .end
88 # Local Variables:
89 #   mode: pir
90 #   fill-column: 100
91 # End:
92 # vim: expandtab shiftwidth=4 ft=pir: