We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Use the Value as a String name
Page Index Toggle Pages: 1
Use the Value as a String name? (Read 391 times)
Use the Value as a String name?
Mar 3rd, 2008, 4:29pm
 
Hello,

i a getting crazy on this, since i can't find an answer:

In a Array i have some sitting som string, and now i would like to turn every second of the entries to a single String, like this:

Array = {"One","Something","Two","Again something"};

And now i would like to have it like this:

String str(Array[0]) = Array[1];

That there is a Variable One which has the value "Something"
One = "Something";

What to do????
Re: Use the Value as a String name?
Reply #1 - Mar 3rd, 2008, 6:09pm
 
You can't do that in processing.
And, TBH, I don't see the use of having a variable name to a variable. Variable names are just identifiers, you shouldn't really be using the name of a variable as part of it's data.

For your example, you'd be much better off creating a small class, and then having an array of them. e.g.
Code:

class thing
{
String name;
String value;
thing(String n, String v)
{
name=n;
value=v;
}
}

String[] foo={"One","Something","Two","Again something"};

thing[] things=new thing[0];
for(int i=0;i<foo.length;i+=2)
{
things=(thing[])append(things,new thing(foo[i],foo[i+1]));
}
Re: Use the Value as a String name?
Reply #2 - Mar 3rd, 2008, 6:31pm
 
Thanks,
but i guess that doesn't work.
See, i have a huge String from a file, which i have no rights to changes anything. And that string hast some Strings and some Numbers. The String always says, what the folowing numbers are. But the count is always different.

So i want to organize it that way, that i load the string and then have a singel Strings whose variable-names are the names from the huge string.

That might look like this:
"Numbers:2,6,3,8,5,8,4,1,2,3,5,7,9,6,44 More:3,4,9,2,4"
And should then be
int Numbers[] = {"2,6,3,8,5,8,4,1,2,3,5,7,9,6,44"};
int More = {"3,4,9,2,4"};
Re: Use the Value as a String name?
Reply #3 - Mar 4th, 2008, 4:12pm
 
Split() splits a string into pieces.
http://processing.org/reference/split_.html

in your case you'll need to split on " " to get each substring then split each on ":" to get the name and value. use the name as hashmap key, the value as the value.

String input = "Numbers:2,6,3,8,5,8,4,1,2,3,5,7,9,6,44 More:3,4,9,2,4";
String nvpairs[] = split(input, " ");
HashMap hash = new HashMap();
for (int i = 0 ; i < nvpairs.length ; i++) {
 String nameval[] = split(nvpairs[i], ":");
 println("Name : " + nameval[0]);
 println("Value: " + nameval[1]);
 hash.put(nameval[0], nameval[1]);
}
Page Index Toggle Pages: 1