cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Translate the entire conversation x

string to number within lookup

Snowshoeman
12-Amethyst

string to number within lookup

Is there a way to imbed the str2num function into a lookup?

 

Is there a means to automate renaming or concatenation of the variables in the first column of the text file?

 

What is the type of variable is ["600"] ?  I guess it's not a string variable?

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Snowshoeman)

Main problem, as Terry already said, is that you did not realize that "lookup" ALWAYS returns a matrix, because there could be more lines in the vector "field" with the same name. So as just one value could be found, the result nonetheless is a 1x1 matrix - hence the square brackets.

 

So you could add a vector/matrix index "ORIGIN" to select only the first (and only) entry of that vector/matrx. Of course if you always use ORIGIN=1 you can replace ORIGIN by the number 1.

Werner_E_0-1752187091300.png

But if you do so with a field name whose corresponding value is not a number in a string, you get an error and the variable remains undefined.

Werner_E_1-1752187183999.png

A possible solution is to wrap a "try ... onerror ..." around to catch that error

Werner_E_2-1752187241148.png

But doing so for every variable you want to assign a value is very tedious.

 

So you may use a different path.

One way could be to turn ALL the values in your value-column into numbers - of course only if that's possible, if the string represents a number (otherwise leave it unchanged).

We can use a small utility function to do the job:

Werner_E_3-1752187372138.png

This function is now called vectorized (mandatory!) with the whole data column as its argument:

Werner_E_4-1752187475931.png

Now all the entries in "value" are ordinary numbers, if possible and you can use "lookup" the normal way (still using vector index ORIGIN or 1 to avoid getting a 1x1 matrix.

Werner_E_5-1752187584633.png

If you make a typo and use a field string which does not exist, you get an error and the variable remains undefined

Werner_E_6-1752187661273.png

This error could also be trapped using a "try...onerror...".

 

So here is a second approach to your problem. Its using a small utility function and does without the vector variables "field" and "value".

Werner_E_7-1752187792471.png

"GetValue" is very similar to Terry's "function". The difference is that is does not make use of the vectors "field" and "value" but rather uses your data matrix S1 right away and "GetValue" also additionally catches the error which you would get in case the argument string provided does not exist.

 

In case you have more two-columns data matrices to deal with, not just S1, you can make the data matrix an additional argument of the function, so you can use the very same function to extract the values from any data matrix you have to use:

Werner_E_0-1752189090114.png

 

 

So Chose you weapons . good luck!

 

And before you ask - no, there is no way to turn a string into a variable name. So its not possible to assign all 253 variables in one go. You will have to do it on a one by one basis.

But maybe you don't need all those variables. You could make use of the "GetValue" function instead, like

Werner_E_0-1752188716840.png

or

Werner_E_1-1752188734348.png

Depends on your needs.

 

Prime 11 sheet attached

View solution in original post

10 REPLIES 10

Hi,

The result  is a matrix with a string as an element of the matrix.

 

Like this:-

Capture.JPG

The index of one gets the "600" from the matrix result of lookup ["600"].  The answer is a matrix as function allows for more than one match to the lookup

Hi

What you really need is a function that can read strings and numbers.

Try this 

Capture.JPG

Thank You!

Werner_E
25-Diamond I
(To:Snowshoeman)

Main problem, as Terry already said, is that you did not realize that "lookup" ALWAYS returns a matrix, because there could be more lines in the vector "field" with the same name. So as just one value could be found, the result nonetheless is a 1x1 matrix - hence the square brackets.

 

So you could add a vector/matrix index "ORIGIN" to select only the first (and only) entry of that vector/matrx. Of course if you always use ORIGIN=1 you can replace ORIGIN by the number 1.

Werner_E_0-1752187091300.png

But if you do so with a field name whose corresponding value is not a number in a string, you get an error and the variable remains undefined.

Werner_E_1-1752187183999.png

A possible solution is to wrap a "try ... onerror ..." around to catch that error

Werner_E_2-1752187241148.png

But doing so for every variable you want to assign a value is very tedious.

 

So you may use a different path.

One way could be to turn ALL the values in your value-column into numbers - of course only if that's possible, if the string represents a number (otherwise leave it unchanged).

We can use a small utility function to do the job:

Werner_E_3-1752187372138.png

This function is now called vectorized (mandatory!) with the whole data column as its argument:

Werner_E_4-1752187475931.png

Now all the entries in "value" are ordinary numbers, if possible and you can use "lookup" the normal way (still using vector index ORIGIN or 1 to avoid getting a 1x1 matrix.

Werner_E_5-1752187584633.png

If you make a typo and use a field string which does not exist, you get an error and the variable remains undefined

Werner_E_6-1752187661273.png

This error could also be trapped using a "try...onerror...".

 

So here is a second approach to your problem. Its using a small utility function and does without the vector variables "field" and "value".

Werner_E_7-1752187792471.png

"GetValue" is very similar to Terry's "function". The difference is that is does not make use of the vectors "field" and "value" but rather uses your data matrix S1 right away and "GetValue" also additionally catches the error which you would get in case the argument string provided does not exist.

 

In case you have more two-columns data matrices to deal with, not just S1, you can make the data matrix an additional argument of the function, so you can use the very same function to extract the values from any data matrix you have to use:

Werner_E_0-1752189090114.png

 

 

So Chose you weapons . good luck!

 

And before you ask - no, there is no way to turn a string into a variable name. So its not possible to assign all 253 variables in one go. You will have to do it on a one by one basis.

But maybe you don't need all those variables. You could make use of the "GetValue" function instead, like

Werner_E_0-1752188716840.png

or

Werner_E_1-1752188734348.png

Depends on your needs.

 

Prime 11 sheet attached

All I have to say is Wow!  Thank you!  I will spend some time picking the weapon as you say.

Thanks everyone.  

 

The purpose of the str2num was so I could do further calculations in the sheet.

1.)  What if I just needed to search S1.txt file for a string value in the second column that matched the string value in the first column and keep the data as a string value ... say CTRW  as  "600", not knowing the index values or position?  Seems like all the lookup functions are associated with a vector or number matrix.

2.)  Is it possible to label the syntax of the commands you are using below?  Say as python, JScript or VBScript?

Werner_E
25-Diamond I
(To:Snowshoeman)

I am not sure if 1) is a question. If yes, which?

In my first approach I turned the strings in the second column into numbers as I thought that this could be convenient for further calculations.

In my second approach (GetValue function) similar to Terry's suggestion the data in S1 remains unchanged, the string "600" is still a string there.

If you need the extracted value as a string, simply don't use "str2num" but I thought you wanted the number to make calculations with it.

The importing thing to notice as Terry an me had wirtten is to be aware that the lookup function always return a 1-column matrix, a vector, weven though it may contain only one entry and that you have to use the vector index to select that entry.

 

ad 2) I don't understand what you mean when you are talking about "labeling"(?) commands.

Are you talking about the advanced controls which can be scripted in VBScript, JScript or Python?

Which control do you have in mind and what should it do?

1.) Thank you!  No, you didn't misunderstand.  There are cases where I need a number.  Now I understand that the GetValue works fine as a user defined function without the str2num block inside the function.  I thought I had to look for a native function.  So, thanks!

 

2.) Regarding the advanced controls.  I guess there is not a lot of depth there.  But what do they most resemble?  VBScript, JScript, Python or something else?

 

Thanks again for your patience 

Werner_E
25-Diamond I
(To:Snowshoeman)

The advanced controls can be scripted in VBScript, JScript and in the current version of Prime also in Python if you have installed Python on your system.

Default is JScript as VBScipt may be deprecated by Microsoft and sheets using Python won't run for people not having installed Python (supported are version 3.7 to 3.13 according to the help).

See Advanced Controls

 

Thank you!  Good information.

Announcements

Top Tags