
Copy link to clipboard
Copied
I have used a series of calculations and a concatenation to build a string. I want to use this string to then retrieve the value of a variable that has already been set.
Here's an example:
function endValue();
var returnVariable //variable for function to return - should have a value of "The Value I want in the End" when returned
var example1 = "already";
var example2 = "Set";
var alreadySet = "The Value I want in the End";
var concatenatedString = example1 + example2;//would have a value of "alreadySet"
// here's where I'm lost. This sets the value of returnVariable to "alreadySet" instead of "The Value I want in the End"
returnVariable = concatenatedString;
return returnVariable;
}
I hope I've explained this well enough. I've tried searching for help, but I'm not even sure what I'm looking for.
1 Correct answer
You can use an associative array:
Copy link to clipboard
Copied
Your variables names and values are quite confusing. Also, your function definition is incorrect. You need to replace the semi-colon in the first line with an opening curly bracket.
I don't really follow what you're trying to do here. In line #7 you set the value of concatenatedString to be "alreadySet". Why are you surprised that that's the assigned to returnVariable in line #8, then?

Copy link to clipboard
Copied
Thank you for taking the time to look at it. I just tried to write a paraphrased code to show you what I'm trying to do. The semicolon was a mistake on the paraphrase - I have a bracket in my real code.
I'm not surprised that the value of concatenatedString is "alreadySet" - that's correct - it's the name of the variable I want to actually get the value of. Now how do I get the value of the variable alreadySet using that string?
If you need more detail to understand where I'm going, I have an extensive list of price codes. Each price code is a variable and the price is the value. I can take details about the products (size, color, etc) and systematically determine the price code for an item using the concatenation. Then, I need to use that price code in order to set the price.
Copy link to clipboard
Copied
That's a very roundabout way of doing things, and not recommended. It is possible, though, using the eval method, which evaluates code on-the-fly. But I would highly recommend you do it differently, for example using literal objects which can have a name that you can access, as well as a set of properties.

Copy link to clipboard
Copied
Ok, thank you, I will look into that. For the time being, I have it working with eval.
Copy link to clipboard
Copied
For future readers of this thread... DON'T USE EVAL... just don't. Not kidding.
Yes. It works... but improper use of eval opens up your code for injection attacks.
Copy link to clipboard
Copied
In PDF forms that's not really a danger as they are heavily sand-boxed, but in general I agree it's not a good programming approach.

Copy link to clipboard
Copied
Ha.. yeah, I'm not too concerned with it because this is an internal document, and trust me when I tell you nobody using it will know how to do anything with code.
Copy link to clipboard
Copied
May be better when you use an array for the price codes.

Copy link to clipboard
Copied
Same question though - how could I use arrays and call the value based on a concatenated string?
For each item, the color, size, shape, and quantity are ALL used to determine the price. So each item has a price code something like ColorSizeShapeQty - which I used as a variable name for the price.
What I do is I first run some code to get the color code, then to get the size, then the shape, then the quantity and I concatenate them all together to go get the value of the price of that item. I'm not sure how I could structure arrays to do this. (PS - I realize a more experienced programmer might make easy work of this, but it's beyond my scope of programming knowledge.)
Copy link to clipboard
Copied
You can use an associative array:

Copy link to clipboard
Copied
Thank you! I've got it working with the associative array!

