5 This page shows some simple examples of using the PSPP module.
6 See L<PSPP> for details on each of the subroutines.
8 =head2 A Simple example
10 This example creates a system file called F<foo.sav>, containing one
11 variable called "id". It contains no data.
15 my $dict = PSPP::Dict->new ();
16 my $var = PSPP::Var->new ($dict, "id");
18 my $sysfile = PSPP::Sysfile->new ("foo.sav", $dict);
22 =head2 A slightly more complex example
24 In this example there are three variables, called "id", "name" and "dob".
25 Their formats are F2.0, A80 and DATETIME17 respectively.
29 my $dict = PSPP::Dict->new ();
30 PSPP::Var->new ($dict, "id",
31 (fmt=>PSPP::Fmt::F, width=>2, decimals=>0) );
33 PSPP::Var->new ($dict, "name", (fmt=>PSPP::Fmt::A, width=>80) );
34 PSPP::Var->new ($dict, "dob", (fmt=>PSPP::Fmt::DATETIME) );
36 my $sysfile = PSPP::Sysfile->new ("foo.sav", $dict);
39 =head2 Changing the properties of variables
41 After a variable has been created, parameters may be set for it.
45 my $dict = PSPP::Dict->new ();
46 my $var1 = PSPP::Var->new ($dict, "id");
48 $var1->set_label ("A unique identifier");
49 $var1->add_value_label (0, "Zero");
50 $var1->add_value_label (1, "One");
53 =head2 Appending data to the file
55 When a file is created, it contains no data. Data is added by
56 appending cases to the file.
58 This example creates a file with 3 cases.
62 my $dict = PSPP::Dict->new ();
63 PSPP::Var->new ($dict, "id",
64 (fmt=>PSPP::Fmt::F, width=>2, decimals=>0) );
66 PSPP::Var->new ($dict, "name", (fmt=>PSPP::Fmt::A, width=>8) );
68 my $sysfile = PSPP::Sysfile->new ("foo.sav", $dict);
70 $sysfile->append_case ( [1, "Alf"] );
71 $sysfile->append_case ( [2, "Bert"] );
72 $sysfile->append_case ( [3, "Charlie"] );
76 =head2 Variables with differing input and output formats
78 By default, a variable's output format corresponds to the input format.
79 However, the output format may be changed after the variable has
82 This example shows how to create a DATETIME variable using the current time
83 as its value. Since pspp uses a different epoch to perl, the constant
84 PSPP::PERL_EPOCH needs to be added to the value returned from time(), in order
85 that it be correctly represented by pspp.
89 my $dict = PSPP::Dict->new ();
91 my $var1 = PSPP::Var->new ($dict, "entrytime",
92 (fmt=>PSPP::Fmt::F) );
94 $var1->set_output_format ( (fmt=>PSPP::Fmt::DATETIME, width=>20) );
96 my $sysfile = PSPP::Sysfile->new ("foo.sav", $dict);
100 $sysfile->append_case ( [ $now + PSPP::PERL_EPOCH] )
101 || die "Cant write case";
107 Data can be read from a system file or other source:
111 my $sf = PSPP::Reader->open ("foo.sav");
113 my $dict = $sf->get_dict ();
116 Once opened, the dictionary can be used like any other.
118 for ($v = 0 ; $v < $dict->get_var_cnt() ; $v++)
120 my $var = $dict->get_var ($v);
122 # Print the variables
123 my $name = $var->get_name ();
124 my $label = $var->get_label ();
125 print "Var: $name, Label: $label\n";
127 # Retrieve and print the value labels
128 my $vl = $var->get_value_labels ();
129 print "$_: $vl->{$_}\n" for keys %$vl;
133 Reading of data must be done sequentially using the C<get_next_case> method.
135 while (my $c = $sf->get_next_case () )
138 for ($v = 0; $v < $dict->get_var_cnt(); $v++)
140 print "val$v: @$c[$v] ";