lots of tests
[pspp] / src / language / expressions / helpers.c
index d23e7fe82b0811773b5a99e378f9986bc3fa393c..d7cde20f98d26cd7b5de93bcb95f072c838a8523 100644 (file)
@@ -447,12 +447,9 @@ replace_string (struct expression *e,
                 struct substring haystack,
                 struct substring needle,
                 struct substring replacement,
-                double n)
+                int n)
 {
-  if (!needle.length
-      || haystack.length < needle.length
-      || n <= 0
-      || n == SYSMIS)
+  if (!needle.length || haystack.length < needle.length || n <= 0)
     return haystack;
 
   struct substring result = alloc_string (e, MAX_STRING);
@@ -510,3 +507,23 @@ median (double *a, size_t n)
           : n % 2 ? a[n / 2]
           : (a[n / 2 - 1] + a[n / 2]) / 2.0);
 }
+
+const struct variable *
+expr_index_vector (const struct expression *e, const struct expr_node *n,
+                   const struct vector *v, double idx)
+{
+  if (idx >= 1 && idx <= vector_get_n_vars (v))
+    return vector_get_var (v, idx - 1);
+
+  msg_at (SE, expr_location (e, n),
+          _("Index outside valid range 1 to %zu, inclusive, for vector %s.  "
+            "The value will be treated as system-missing."),
+          vector_get_n_vars (v), vector_get_name (v));
+  if (idx == SYSMIS)
+    msg_at (SN, expr_location (e, n->args[0]),
+            _("The index is system-missing."));
+  else
+    msg_at (SN, expr_location (e, n->args[0]),
+            _("The index has value %g."), idx);
+  return NULL;
+}