Post Reply 
Create a list of undefined user vars?
01-24-2016, 10:18 PM
Post: #1
Create a list of undefined user vars?
In [HOME], can a list of undefined variables somehow be created (for later use)?

Example:
dogs:=0; cats:=0; // define vars first
L1:={'dogs', 'cats'};

This works: INPUT({dogs, cats});

This doesn't work: INPUT(L1); // even though L1 ==> {dogs, cats} same as above. (Why not?)

-Dale-
Find all posts by this user
Quote this message in a reply
01-25-2016, 07:49 AM
Post: #2
RE: Create a list of undefined user vars?
Hello,

Yes, you can create a list of undefined variables, to do so, you need to use the CAS (which can do it), and transform the CAS result in a home result.
For example L1:=CAS("{a, b, c, d}") will work.

Now, the question about the INPUT(L1) not working is a different one.
INPUT(A), as you know asks you to input a value for the variable A.
therefore INPUT(L1) should ask you a value for variable L1, not for the variables contained in the list stored in L1.
It's a case of 'you can not have the butter and the money from the butter'...

The first parameter of INPUT is one of these special cases where the parameter is NOT evaluated by the function.

So, how do you deal with the situation?
Well, one way is to generate an INPUT call with the CONTENT of L1 as a parameter. exrp("INPUT("+string(L1)+")"); will do that.
It creates a string that contains the command and then executes it!

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
01-25-2016, 11:08 AM
Post: #3
RE: Create a list of undefined user vars?
Thank you, Cyrille. I really appreciate your helpful information!

I knew to use the symbolic side, [CAS], to create the list L1, which is why I was seeking a solution based on [HOME]. I wasn't able use the CAS result though, since it was saved in the variable L1. As shown in my example, L1 couldn't be evaluated as an INPUT() parameter, as you have explained. I tried many things, but I didn't know about your string-based approach, and will give that a try!

If anyone is interested, I want to gather input from a changeable list of stock symbols. The idea attempts to make use of "ticker symbols" as "undefined" variable names for an investment progress, portfolio tracker. If a list of symbols, with associated share details, were available, (L1, maintained outside the program), then I could programmatically manage the rest of the portfolio tracking details, needing only current market prices as additional input. The Prime's portability makes it a handy tool for this quick status project.

I've found that there's almost always a way to do things with the Prime. I ran out of ideas here, though!

-Dale-
Find all posts by this user
Quote this message in a reply
01-25-2016, 12:23 PM
Post: #4
RE: Create a list of undefined user vars?
(01-25-2016 07:49 AM)cyrille de brébisson Wrote:  So, how do you deal with the situation?
Well, one way is to generate an INPUT call with the CONTENT of L1 as a parameter. exrp("INPUT("+string(L1)+")"); will do that.
It creates a string that contains the command and then executes it!
Cyrille

Cyrille, could you please elaborate on:

exrp("INPUT("+string(L1)+")"); // ==> results in Syntax Error

no info in exrp() function in help, etc.

Thanks,

Dale-
Find all posts by this user
Quote this message in a reply
01-25-2016, 12:45 PM
Post: #5
RE: Create a list of undefined user vars?
(01-25-2016 12:23 PM)DrD Wrote:  Cyrille, could you please elaborate on:

exrp("INPUT("+string(L1)+")"); // ==> results in Syntax Error

no info in exrp() function in help, etc.

Try with EXPR instead of exrp Wink
Find all posts by this user
Quote this message in a reply
01-25-2016, 02:58 PM
Post: #6
RE: Create a list of undefined user vars?
Of course, thanks Didier. I should have figured that one out. (Flu bug's got me, not my best day).

-Dale-
Find all posts by this user
Quote this message in a reply
01-25-2016, 04:18 PM
Post: #7
RE: Create a list of undefined user vars?
[HOME] Example: Input '5' dogs and '4' cats (quotes required by the INPUT function). I don't understand how the dogs and cats variables' content gets accessed? It's close with this format, but not quite there, yet.

Code:

EXPORT test()
BEGIN
  // !!CREATE IN CAS FIRST!! ==> L1:={'dogs', 'cats'};

  CAS.expr("INPUT("+string(L1)+")");   // Input type requires '5' and '4' (quoted numbers)   
  return eval(L1);  //  How to access contents of {dogs,cats} ==> {5,4}??

END;

-Dale-
Find all posts by this user
Quote this message in a reply
01-25-2016, 05:45 PM (This post was last modified: 01-25-2016 05:46 PM by toml_12953.)
Post: #8
RE: Create a list of undefined user vars?
(01-25-2016 04:18 PM)DrD Wrote:  [HOME] Example: Input '5' dogs and '4' cats (quotes required by the INPUT function). I don't understand how the dogs and cats variables' content gets accessed? It's close with this format, but not quite there, yet.

Code:

EXPORT test()
BEGIN
  // !!CREATE IN CAS FIRST!! ==> L1:={'dogs', 'cats'};

  CAS.expr("INPUT("+string(L1)+")");   // Input type requires '5' and '4' (quoted numbers)   
  return eval(L1);  //  How to access contents of {dogs,cats} ==> {5,4}??

END;

-Dale-

I can't get it to work at all. My line looks like this:

CAS.expr("INPUT("+string(L1)+")");

and I get a syntax error with the cursor between the first open parenthesis and the quote mark.

Tom L

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
01-26-2016, 06:01 AM
Post: #9
RE: Create a list of undefined user vars?
Hello,

1) At the time of the INPUT, the inputed variables MUST exist as INPUT uses the current value of the variable as the default value.

2) do not use CAS.INPUT, INPUT is NOT a CAS command...

3) What is the value of L1? can you show it?

4) to help you debug, you can save the string in a temporary variable and show it on the screen (PRINT(string_variable); WAITWink or just (save the string in a user accessible variable and kill the program). This will probably let you see what is happening.


Good luck.

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
01-26-2016, 12:08 PM (This post was last modified: 01-26-2016 12:12 PM by DrD.)
Post: #10
RE: Create a list of undefined user vars?
(01-26-2016 06:01 AM)cyrille de brébisson Wrote:  Hello,

1) At the time of the INPUT, the inputed variables MUST exist as INPUT uses the current value of the variable as the default value.
Well, this brings us full circle: Creating a list of undefined variables IS the whole purpose of the effort.

2) do not use CAS.INPUT, INPUT is NOT a CAS command...
The "CAS." prefix was used in order to create a list containing symbolic content (only available from the CAS side), and necessary for naming undefined variables. This is from your earlier suggestion to use CAS to initially create the symbolic name list. (It doesn't work without it). Based on the information you provided yesterday, this sort of worked, to the extent that the INPUT function was able to (sort of) work with it.

EXAMPLE: Make sure User Variables, dogs and cats, don't exist in User Variable memory, then, (in CAS!), create this list: L1:={'dogs', 'cats'}, and run this program:

Code:

EXPORT test()
BEGIN
  // !!CREATE IN CAS FIRST!! ==> L1:={'dogs', 'cats'};
  CAS.expr("INPUT("+string(L1)+")");   // Input type requires '5' and '4' (quoted numbers)   
  return eval(L1);  //  How to access contents of {dogs,cats} ==> {5,4}??
END;


3) What is the value of L1? can you show it?
The Example given above for list L1 was: L1:={'dogs', 'cats'}; however, ANY list containing undefined variable names (that are not reserved variable names) is the ultimate goal. Then, (from your suggestion): CAS.expr("INPUT("+string(L1)+")"); use real number values for the dogs and cats, (previously undefined), as inputs. In the example given, I used a value (for dogs) of 5, and (for cats) of 4. However, this construct only allows quoted numbers as inputs ('5' and '4'), which seem to be unavailable after the INPUT function has completed. I don't understand this, which is why I said the construct "sort of" works.

4) to help you debug, you can save the string in a temporary variable and show it on the screen (PRINT(string_variable); WAITWink or just (save the string in a user accessible variable and kill the program). This will probably let you see what is happening.
My objective, to provide a list of undefined variables, subsequently defined via the INPUT(list) function, may not be possible.

It's useful, in my case, because I want to pre-condition L1 with stock symbol data, to be used, later on, in a generic program that provides statistics of interest using that changeable list; such as, "unrealized gain", "portfolio net worth," etc. Your suggestion comes VERY close to what is needed; but, after spending, (probably too much), time, I haven't quite gotten over this hump yet!

Thanks for your ideas, Cyrille!! Hopefully, others can also gain benefit from information you've provided, even if this notion bumps up against a dead end! -Dale-


Good luck.
Find all posts by this user
Quote this message in a reply
01-27-2016, 05:59 AM
Post: #11
RE: Create a list of undefined user vars?
Hello,

I think that I am starting to understand what you are trying to do.
you want cats and dog to be defined BY the INPUT function.

Unfortunately, this can not work. INPUT requires you to have the variable available and existing at the time it is called.

HVars is a function that can let you programaticaly create variables (ie, a name+value pair).

You can then use them as input for INPUT (using the string trick)

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
01-27-2016, 12:27 PM
Post: #12
RE: Create a list of undefined user vars?
Using a list of undefined variable names, created and maintained without programmatic influence, is my specific objective. It's kind of like a database list, I guess. I'll try using the HVars approach next. That's a good idea, and one I haven't tried. There's more than one way to "skin" a prime - at least if you're using the EMU version!

Thanks (again), Cyrille!

-Dale-
Find all posts by this user
Quote this message in a reply
01-27-2016, 04:13 PM (This post was last modified: 01-27-2016 04:15 PM by Didier Lachieze.)
Post: #13
RE: Create a list of undefined user vars?
(01-27-2016 12:27 PM)DrD Wrote:  Using a list of undefined variable names, created and maintained without programmatic influence, is my specific objective. It's kind of like a database list, I guess. I'll try using the HVars approach next. That's a good idea, and one I haven't tried. There's more than one way to "skin" a prime - at least if you're using the EMU version!

Thanks (again), Cyrille!

-Dale-

I've played a bit with these different ideas and so here is a version of your test program where a list of undefined variables names is used to drive the INPUT statement and to retrieve the values:

Code:
EXPORT test()
BEGIN
  LOCAL j,l,s,v;
  
  //variables set-up
  v:={"dogs", "cats"};   //list of your undefined variables, replace with whatever list you want

  FOR j FROM 1 TO SIZE(v) DO IFTE(POS(HVars,v(j)),DelHVars(v(j)),0); END;  //ensure variables dogs & cats don't exist
  s:="L1:="+REPLACE(STRING(v),"""","'");  CAS(s);  // !!CREATE L1 IN CAS FIRST!!  ==> L1:={'dogs', 'cats'};
  FOR j FROM 1 TO SIZE(v) DO HVars(v(j)):=0; END;   //create variables dogs & cats for the INPUT statement, assuming that they are reals
  
  //Input statement
  EXPR("INPUT("+string(L1)+")");
  l:=EVAL(L1);    // get contents of {dogs,cats} 

  //Variable clean-up
  FOR j FROM 1 TO SIZE(v) DO DelHVars(v(j)); END;    //delete variables dogs & cats
  L1:={};    //clear L1

  return l;    //returns contents of {dogs,cats} ==> {5,4}
END;
Find all posts by this user
Quote this message in a reply
01-27-2016, 04:42 PM (This post was last modified: 01-27-2016 04:47 PM by DrD.)
Post: #14
RE: Create a list of undefined user vars?
Wow. That's a bunch of prep to get the list in order! I was also trying Cyrille's idea of using HVars for this. It might be simpler. I can get the HVar list, but (so far) I haven't been able to exchange any numerical data with the variables.

Here's an abbreviated example:
Code:

L1:={'CAT',5000,58.95,'IBM',2000,122.14};  // {symbol, #shares, cost, ...} Create in CAS!
HVars("Tickers"):={L1(1),L1(4)};  //  Home variable Tickers == symbol list == {CAT,IBM}

I've tried several ways to use Tickers but no luck. Any ideas?

-Dale-
Find all posts by this user
Quote this message in a reply
01-27-2016, 04:48 PM
Post: #15
RE: Create a list of undefined user vars?
What do you want to do with Tickers?
Find all posts by this user
Quote this message in a reply
01-27-2016, 04:54 PM (This post was last modified: 01-27-2016 05:11 PM by DrD.)
Post: #16
RE: Create a list of undefined user vars?
Tickers == {CAT, IBM} Each element of the list would be used to hold the current price. Lets say, the current price of Tickers(1), CAT was $60, and Tickers(2), IBM was $123. If INPUT(Tickers) would work, the idea would be to have an input field for the variable CAT, which would receive the current price ($60), and similarly for the variable IBM at $123.

This would allow the use of CAT and IBM as variables, allowing further computation as needed. CAT == 60, and IBM == 123, so 2*CAT = 120, etc.
Find all posts by this user
Quote this message in a reply
01-27-2016, 05:10 PM
Post: #17
RE: Create a list of undefined user vars?
Something like that?

Code:
EXPORT test2()
BEGIN
  LOCAL j;
  HVars("Tickers"):={L1(1),L1(4)};  //  Home variable Tickers == symbol list == {CAT,IBM}
  FOR j FROM 1 TO SIZE(Tickers) DO HVars(STRING(Tickers(j))):=L1(3*j); END;   //create variables for the INPUT statement with their initial values
  EXPR("INPUT("+string(Tickers)+")");
END;
Find all posts by this user
Quote this message in a reply
01-27-2016, 05:17 PM
Post: #18
RE: Create a list of undefined user vars?
YES! I think you've done it. I'll try to digest what's going on there, and move on to the next dilemma. Thanks for the assist! (I just knew there had to be a way to do this...)

-Dale-
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 3 Guest(s)