pivot-table: Honor blank variable labels.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 17 Feb 2023 16:39:35 +0000 (08:39 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 17 Feb 2023 17:11:18 +0000 (09:11 -0800)
pivot_value_new_variable() ignored empty-string variable labels.  This
honors them.

Requested by Frans Houweling.

doc/variables.texi
src/data/variable.c
src/output/pivot-table.c
tests/automake.mk
tests/language/commands/variable-labels.at [new file with mode: 0644]

index 6a033db42bffedbf227f9184c8942bfda84c681d..c1b18edfa600c68923374bc2dd39ac2a76beaf9b 100644 (file)
@@ -293,30 +293,26 @@ dictionary; use @cmd{NEW FILE} to do that (@pxref{NEW FILE}).
 @section VARIABLE LABELS
 @vindex VARIABLE LABELS
 
-In addition to a variable's name, each variable can have a @dfn{label}.
-Whereas the name is limited to certain constraints (@pxref{Attributes}) a variable's
-label has no such constraints.
-Typically, the names are concise, easy to type mnemonics for the variable
-and the labels are longer, more verbose descriptions.
+In addition to a variable's name, each variable can have a
+@dfn{label}.  Whereas a variable name is a concise, easy-to-type
+mnemonic for the variable, a label may be longer and more descriptive.
 
 @display
 VARIABLE LABELS
-        @var{var_list} '@var{var_label}'
-        [ /@var{var_list} '@var{var_label}']
-        .
-        .
-        .
-        [ /@var{var_list} '@var{var_label}']
+        @var{variable} '@var{label}'
+        [@var{variable} '@var{label}']@dots{}
 @end display
 
 @cmd{VARIABLE LABELS} associates explanatory names
 with variables.  This name, called a @dfn{variable label}, is displayed by
 statistical procedures.
 
-To assign a variable label to a group of variables, specify a
-list of variable names and the variable label as a string.
-To assign different labels to different variables in the same command,
-precede the subsequent variable list with a slash (@samp{/}).
+Specify each variable followed by its label as a quoted string.
+Variable-label pairs may be separated by an optional slash @samp{/}.
+
+If a listed variable already has a label, the new one replaces it.
+Specifying an empty string as the label, e.g.@:@samp{''}, removes a
+label.
 
 @node PRINT FORMATS
 @section PRINT FORMATS
index 87e7d07823f42271707000938d7501a24f562250..4fdfdcbb65d83609b14f8f29b14bf1ffe6b0741a 100644 (file)
@@ -760,23 +760,20 @@ var_get_label (const struct variable *v)
 
 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
    and trailing white space.  If LABEL is a null pointer or if LABEL is an
-   empty string (after stripping white space), then V's variable label (if any)
-   is removed. */
+   empty string, then V's variable label (if any) is removed. */
 static void
 var_set_label_quiet (struct variable *v, const char *label)
 {
   free (v->label);
   v->label = NULL;
 
-  if (label != NULL && label[strspn (label, CC_SPACES)])
+  if (label != NULL && label[0])
     v->label = xstrdup (label);
 
   ds_destroy (&v->name_and_label);
   ds_init_empty (&v->name_and_label);
 }
 
-
-
 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
    and trailing white space.  If LABEL is a null pointer or if LABEL is an
    empty string (after stripping white space), then V's variable label (if any)
@@ -789,7 +786,6 @@ var_set_label (struct variable *v, const char *label)
   dict_var_changed (v, VAR_TRAIT_LABEL, ov);
 }
 
-
 /* Removes any variable label from V. */
 void
 var_clear_label (struct variable *v)
index 661813f28b22d0522a8fdbe5465bdf71796f95b3..bec8638bd03a037a3a096b2209968e6d0999486e 100644 (file)
@@ -2915,7 +2915,7 @@ pivot_value_new_variable__ (const char *name, const char *label)
     .variable = {
       .type = PIVOT_VALUE_VARIABLE,
       .var_name = xstrdup (name),
-      .var_label = xstrdup_if_nonempty (label),
+      .var_label = xstrdup_if_nonnull (label),
     },
   };
   return value;
index 32e083e75291fbf5f23ac430ed653b01c6da50ca..41d8edba737da7cb3ad1ccaf384430d921e14858 100644 (file)
@@ -434,6 +434,7 @@ TESTSUITE_AT = \
        tests/language/commands/title.at \
        tests/language/commands/update.at \
        tests/language/commands/value-labels.at \
+       tests/language/commands/variable-labels.at \
        tests/language/commands/variable-display.at \
        tests/language/commands/vector.at \
        tests/language/commands/weight.at \
diff --git a/tests/language/commands/variable-labels.at b/tests/language/commands/variable-labels.at
new file mode 100644 (file)
index 0000000..aefc002
--- /dev/null
@@ -0,0 +1,25 @@
+AT_BANNER([VARIABLE LABELS])
+
+AT_SETUP([VARIABLE LABELS])
+AT_DATA([var-labels.sps], [dnl
+DATA LIST LIST NOTABLE/x y z.
+VARIABLE LABELS x 'First variable' y ' '.
+BEGIN DATA.
+1 2 3
+4 5 6
+END DATA.
+DESCRIPTIVES x y z.
+])
+AT_CHECK([pspp -O box=unicode var-labels.sps], [0], [dnl
+                Descriptive Statistics
+╭────────────────────┬─┬────┬───────┬───────┬───────╮
+│                    │N│Mean│Std Dev│Minimum│Maximum│
+├────────────────────┼─┼────┼───────┼───────┼───────┤
+│First variable      │2│2.50│   2.12│   1.00│   4.00│
+│                    │2│3.50│   2.12│   2.00│   5.00│
+│z                   │2│4.50│   2.12│   3.00│   6.00│
+│Valid N (listwise)  │2│    │       │       │       │
+│Missing N (listwise)│0│    │       │       │       │
+╰────────────────────┴─┴────┴───────┴───────┴───────╯
+])
+AT_CLEANUP
\ No newline at end of file