Message Boards Message Boards

0
|
2836 Views
|
1 Reply
|
2 Total Likes
View groups...
Share
Share this post:

String manipulation not working

Posted 3 years ago

I have a for loop that iterates over an array of movies that a person has played in, and it outs that output together. This is the code:

p = Interpreter["Person"]["Einstein"];
MovieAppearancesParsed = "";
MovieAppearances = EntityValue[p, "MovieAppearances"];
For[i = 0, i < Length[MovieAppearances], i++,MovieAppearancesParsed <>";;"<> ToString[EntityValue[Part[MovieAppearances, i],"Name"]]]
Print[MovieAppearancesParsed]

that doesn't work, MovieAppearancesParsed doesn't get changed at all I can, however, do this:

p = Interpreter["Person"]["Einstein"];
MovieAppearancesParsed = "test";
MovieAppearances = EntityValue[p, "MovieAppearances"];
For[i = 0, i < Length[MovieAppearances], i++ ,Print[EntityValue[Part[MovieAppearances, i],"Name"]]]
Print[MovieAppearancesParsed]

but that prints this: " EntityValue[List,Name] Der ewige Jude Naqoyqatsi Trinity and Beyond: The Atomic Bomb Movie Atomic Power test "

why can't I use the first approach? I can't use the last one, as I format the output and there is other stuff that I need to format with it.

POSTED BY: Faas Tubee
Posted 3 years ago

Hi Faas,

MovieAppearancesParseddoesnt get changed at all

Because there is no assignment to it in the loop

Some beginner WL advice

  • Avoid using symbol names that start with an uppercase letter, they are used by WL built-ins.
  • Use functional constructs (e.g. Map) rather than procedural constructs (e.g. For).
  • Decouple data collection and processing
    p = Interpreter["Person"]["Einstein"];
    movieAppearances = EntityValue[p, "MovieAppearances"];

    movieNames = #["Name"] & /@ movieAppearances
    (*
    {"Der ewige Jude", "Naqoyqatsi", "Trinity and Beyond: The Atomic Bomb Movie", "Atomic Power", 
    "The Yellow Star – The Persecution of the Jews in Europe 1933–45"}
    *)

Single String with names delimited by ;;

StringRiffle[movies, ";;"]
(* Der ewige Jude;;Naqoyqatsi;;Trinity and Beyond: The Atomic Bomb Movie;;Atomic Power;;The Yellow Star – The Persecution of the Jews in Europe 1933–45 *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract