- Sep 29, 2000
- 2,157
- 0
- 0
Okay, say I have a text file with a whole bunch of config settings
eg:
param1=foo
param2=bar
...
paramn=at_rocks
and I want to store these as variables param1, param2... in my program.
With every common language I've seen (C, Java, perl, etc.) to do this, you had to do something like:
while (read in a line)
{
String param = getBitBeforeEquals(line);
String value = getBitAfterEquals(line);
if (param == "param1")
param1 = value;
if (param == "param2")
param2 = value;
....
if (param == "paramn")
paramn = value;
}
Now, this strikes me as grossly inefficient since your running on O(n^2) time instead of O(n).
Is there any language where I could do something like:
while (read in a line)
{
String param = getBitBeforeEquals(line);
String value = getBitAfterEquals(line);
setParam(param, value);
}
so that given a string, the language would return the variable that has that name?
Similarly, could I do the same thing with functions. I've run into the problem quite frequently where I get something like:
if (dir = "left")
{
goLeft();
print("I have turned left");
}
if (dir = "right")
{
goRight();
print("I have turned left");
}
if (dir = "back")
{
goForward();
print("I have turned back");
}
The problem is, if I want to change the code, say add an instruction between goX() and print, I need to modify the code 3 times and errors inevitably creep in. Are there any languages that support something like:
String dir_tmp = dir;
dir_tmp[0] = toUpper(dir[0]); //Capitalises 1st letter
runFunction("go" + dir_tmp);
print ("I have turned " + dir);
eg:
param1=foo
param2=bar
...
paramn=at_rocks
and I want to store these as variables param1, param2... in my program.
With every common language I've seen (C, Java, perl, etc.) to do this, you had to do something like:
while (read in a line)
{
String param = getBitBeforeEquals(line);
String value = getBitAfterEquals(line);
if (param == "param1")
param1 = value;
if (param == "param2")
param2 = value;
....
if (param == "paramn")
paramn = value;
}
Now, this strikes me as grossly inefficient since your running on O(n^2) time instead of O(n).
Is there any language where I could do something like:
while (read in a line)
{
String param = getBitBeforeEquals(line);
String value = getBitAfterEquals(line);
setParam(param, value);
}
so that given a string, the language would return the variable that has that name?
Similarly, could I do the same thing with functions. I've run into the problem quite frequently where I get something like:
if (dir = "left")
{
goLeft();
print("I have turned left");
}
if (dir = "right")
{
goRight();
print("I have turned left");
}
if (dir = "back")
{
goForward();
print("I have turned back");
}
The problem is, if I want to change the code, say add an instruction between goX() and print, I need to modify the code 3 times and errors inevitably creep in. Are there any languages that support something like:
String dir_tmp = dir;
dir_tmp[0] = toUpper(dir[0]); //Capitalises 1st letter
runFunction("go" + dir_tmp);
print ("I have turned " + dir);