9.6 The $ Function

9.6.1 Synopsis

$ (expression)

9.6.2 Description

Expression is evaluated and reduced to a string. After that, $ (expression) is replaced by the value of a variable with such a name. You can use the $ function everywhere a standard variable is allowed (assignments, conditional expressions, arrays ...).

This feature gives a high level of flexibility since you can build variable names within your scripts.

9.6.3 Example

var_1 = "Some text";
var_name = "var_1";

Now, var_name evaluates to var_1 and $ (var_name) evaluates to the value of variable var_1. In practice, the following statements:

print (var_1);
print (var_name);
print ($ (var_name));

would display:

Some text
var_1
Some text

You can use $ function within conditional expressions:

if ($ (var_name) == "Some text") {
  print ("The squirrel that you kill in jest, dies in earnest.");
  print ("-- Henry David Thoreau");
}

Output will be:

The squirrel that you kill in jest, dies in earnest.
-- Henry David Thoreau

You can also use $ function to assign variables: let's overwrite value stored within var_1:

$ (var_name) = "Some other text";

9.6.4 Example (2)

The $ function applies to every valid Cows expression; some assignments follows:

name = "foo_";
$ (name + "1") = "When I was 12, I went hunting with my father\n";
$ (name + "2") = "and we shot a bird. He was laying there and\n";
$ (name + "3") = "something struck me. Why do we call this fun to\n";
$ (name + "4") = "kill this creature [who] was as happy as I was when\n";
$ (name + "5") = "I woke up this morning.\n";
$ (name + "6") = "-- Marv Levy (football head coach)";
print (foo_1 + foo_2 + foo_3 + foo_4 + foo_5 + foo_6);

Cows' output will be:

When I was 12, I went hunting with my father
and we shot a bird. He was laying there and
something struck me. Why do we call this fun to
kill this creature [who] was as happy as I was when
I woke up this morning.
-- Marv Levy (football head coach)

The $ function also applies to itself:

variable = "Some text";
var_name = "variable";
var_name_name = "var_name";

Now variable, $ (var_name) and $ ($ (var_name_name)) evaluate to Some text.

Similar considerations hold for arrays (We'll talk about arrays later so don't worry if you don't know about them yet).

my_array_1 [ ] = {
  "You ask people why they have deer heads on the wall.",
  "They always say, 'Because it's such a beautiful animal.'",
  "There you go. I think my mother's attractive, but I have",
  "photographs of her.",
  "-- Ellen DeGeneres"
};
name = "my_array";
id = "1";
ifdef ( $ (name + "_" + id) [ ] ) {
  print ($ (name + "_" + id) [0]);
  print ($ (name + "_" + id) [1]);
  print ($ (name + "_" + id) [2]);
  print ($ (name + "_" + id) [3]);
  print ($ (name + "_" + id) [4]);
}

Cows' output will be:

You ask people why they have deer heads on the wall.
They always say, 'Because it's such a beautiful animal.'
There you go. I think my mother's attractive, but I have
photographs of her.
-- Ellen DeGeneres

This manual can be downloaded from http://www.g-cows.org/.