What languages can do this?

Shalmanese

Platinum Member
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);
 

uec0

Junior Member
Jun 14, 2004
10
0
0
Java:

Hashtable h = new Hashtable();

while ( <next line exists> ) {
String s = <read in line>;
String[] sa = s.split(&quot;=&quot;);

if (h.containsKey(sa[0])) {
h.remove(sa[0]);
}

h.put(sa[0], sa[1]);
}


It's even easier in C#, but I don't know the code offhand.

Of course, the code is untested, unapproved, and definitely not robust, but I think it would work for your first task.
 

Spencer278

Diamond Member
Oct 11, 2002
3,637
0
0
What you want is rather unsupported in complied languages because they are complied so you will lose the function name when run. If you use an interpretive language and make the file format the same as the language then it because trivial. Like if you where using scheme
(assign? variable expression)
and after sourcing the file with all the commands you want you could use the initialized variables without doing anything.

The best solution to your problem with reading none code and assuming you have an OO language you can use I would make one onject do all the text crap and then inherite from that object.
like
class myObjectThatDoesBS

void goDirection( string dirctetion ) {
if( direction == "left" ) {
left();
else
...
}

then you just ignore the crap and use the function left().
 

Shalmanese

Platinum Member
Sep 29, 2000
2,157
0
0
A hashtable would be a good answer in any OO language but Java. Putting stuff in and taking stuff our of Java data structures is a major pain. In any case, that's not what I want. It's not so much the solution as a concept, does any language treat variables as true first class objects that can be abstracted away into a name and a value. The same with the goDirection, it was just an example to illustrate a case.

Where does it say that compiled languages must neccesarily throw away function names? Why can't it be something like:

public abstract class Variable extends Object
{
public Variable (String Name, Type type);

public set (Object a);

public Object get();
}
public abstract class Function extends Object
{
public Function (String name, Type returnValue, Type[] Arguments, String code);

public Object run (Object[] Arguments);
}

Once you've abstracted it away like that, then you can do the stuff I mentioned above. I was wondering if any languages have chosen to go down this route.
 

Ninjazx

Member
May 29, 2004
122
0
76
Originally posted by: Shalmanese

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;
}

Although I agree it seems there should be an easier way to do this (hashing for instance), hat may not be as in-efficient as you think.

Since it is only running through if statements for each line, the ACTUAL count (not O estimations) would be (X * Y) where
X = number Of Lines read
Y = number Of If Statements in loop

A switch statement would reduce Y by 1/2... but thats not the fix your looking for :(
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
You can do it in C (obviously... it's the greatest and most powerful portable language ever ;)), and in assembly. It's not as trivial as you'd like... but you can do it. I've never actually written code to do it, but I'd probably do it in a method similar to the "interrupt vector" of x86 processors. When the CPU executes an instruction like "INT 21h", it goes to a table (interrupt table), reads entry 0x21, and starts executing the code at that location. In C, you'd instead have a table of function pointers. I don't recall all the details on function pointers... but something like this should work:

void *funcs[];

funcs = (void *)malloc(4*sizeof(void *)); /* allocate 4 pointers */
funcs[0] = &amp;goLeft;
funcs[1] = &amp;goRight;
funcs[2] = &amp;goUp;
funcs[3] = &amp;goDown;

Then, use the parameter to index into the function array and call that function. Using enum datatypes might make it easier to map the parameters to the array indicies (just a guess which I haven't thought through)

Google proved very informative when I searched for function pointer C. This example is good, and probably very similar to what you want.

Java's lack of pointers sucks ;).
 

Sahakiel

Golden Member
Oct 19, 2001
1,746
0
86
Originally posted by: Shalmanese
Where does it say that compiled languages must neccesarily throw away function names?.

This is just a guess, but probably around the part where it translates your functions into assembly instructions or assembly into binary.
 

Shalmanese

Platinum Member
Sep 29, 2000
2,157
0
0
Yes, well obviously most current compiled languages do that but theres no reason why it's can't store the function name as an associated meta-data for the function.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Originally posted by: Shalmanese
Yes, well obviously most current compiled languages do that but theres no reason why it's can't store the function name as an associated meta-data for the function.

It's stored, for debug binaries... it just isn't used. There's no reason to keep it around, and it wastes space to store.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: Shalmanese
Yes, well obviously most current compiled languages do that but theres no reason why it's can't store the function name as an associated meta-data for the function.

Well, I guess you *could*, but it's not a very robust solution. If you change the name of one of your functions, now you have to go back and change everything else, too. Note that this behavior is common in functional-programming and list-programming languages like Scheme and LISP!

Function pointers are the "right" way to do this in a compiled language like C.

However, what you seem to be looking for is a way around having to map the name to the function. There is no way computationally to do this. You said:

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);

But the code for runFunction() has to take the string you give it and figure out which function it belongs to somehow. You *can* do this in C or C++, by having a hash table (or other indexable data structure) that maps <String/char * , (function prototype type)*> pairs. But there's no way around having to do some sort of search, either implicitly (as in Scheme) or explicitly (as with this method in C/C++). I've never mucked with function pointers in Java, but there may be a way to access them, or else you can do as suggested above and write a class that abstracts this functionality away:

class myObjectThatDoesBS
{

void goDirection( string dirctetion ) {
if( direction == "left" ) {
left();
else if( direction == "right" ) {
...
}
}

}

Then you just read in the string and call 'variable_name.goDirection( input_string )'.
 

cquark

Golden Member
Apr 4, 2004
1,741
0
0
The config file read problem is simple in perl:

while (<>) {
my ($lhs,$rhs) = split(/=/, $_);
$$lhs = $rhs;
}

After that loop, there is a variable in your program for every variable name appearing to the left of the equal sign.

I generally prefer to store all the variables in a hash, which prevents overwriting an existing variable name with one from the config file:

my %config;
while (<>) {
my ($lhs, $rhs) = split(/=/, $_);
$config{$lhs} = $rhs;
}

Perl can also insert functions and classes by name too, solving your second problem, but I don't have time to write an example for you right now. Check out Advanced Perl Programming. There are ways of solving the second problem in python too, since classes are just another type of object in python.