Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.1K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to assign numerical values to a list of strings?

Posted 11 years ago

(*I am a novice in Mathematica,and am finding a hard time in \ associating numerical values with a list of strings.Here is what I \ have been trying to do:*) list = ReadList[ "file path", Word, WordSeparators -> {","}];

(the list is a single column of several multi-character variables) cLength = Length[list]; cLength For[i = 1, i < cLength, i++, Keys[<|list[[i]] -> (0.0 + 0.005*i)|>]; Print[list[[i]]]]; x = 1; y = 2; x y

(* I get the below. While assigning "1" to x, and "2" to y, gives the correct association, The code above does not assign the numerical values to variable1 and variable2 as instructed in the "For" loop)

177

variable1 variable2 .... 1 2

Attachments:
POSTED BY: Randa Nakib
4 Replies

It's not clear to me what you wish to do from your notebook, so I will just offer a few observations. First, just the advice to move away from procedural programming. It's not the "Mathematica way." And doing so will help you move forward in learning more about how Mathematica works and its power.

One cannot assign a value to a string (also the assignment operator is =, not ==) the following will not work

"aa"=1.2

If you want to convert a string into an expression use the function ToExpression.

Finally, I recommend taking a step back and spending some reading Paul Wellin's book.

I hope this helps. --David

POSTED BY: David Reiss
Posted 11 years ago

Hello Dr. Reiss, I need to be able to read a string list from a file. The list could have a large number of a limited number of elements (aa,bb,cc,dd,ee). For example, the list could be:

                                 {aa,bb,cc,dd,ee,ee,dd,cc,bb,aa,aa,cc,ee,dd,aa,aa}

I want to be able to assign the values of the elements red from the file to values obtained within the program, and then repopulate the list with the numerical value assigned to each element. I could not succeed with the association method you showed me.

Also, it seems Mathematica adds a double quote to each element when read from a text file. If true, how would I remove them?

Thank you

Attachments:
POSTED BY: Randa Nakib
Posted 11 years ago
POSTED BY: Randa Nakib
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard