Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to Reference a Variable with a String

Guest
Oct 12, 2017 Oct 12, 2017

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.

TOPICS
Acrobat SDK and JavaScript , Windows
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 16, 2017 Oct 16, 2017

You can use an associative array:

JavaScript Arrays

Translate
Community Expert ,
Oct 12, 2017 Oct 12, 2017

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 12, 2017 Oct 12, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2017 Oct 12, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 12, 2017 Oct 12, 2017

Ok, thank you, I will look into that.  For the time being, I have it working with eval. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2017 Oct 12, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 13, 2017 Oct 13, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 16, 2017 Oct 16, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 13, 2017 Oct 13, 2017

May be better when you use an array for the price codes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 16, 2017 Oct 16, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 16, 2017 Oct 16, 2017

You can use an associative array:

JavaScript Arrays

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 17, 2017 Oct 17, 2017
LATEST

Thank you!  I've got it working with the associative array!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines