Message Boards Message Boards

GROUPS:

Why is AppendTo "Squaring My Strings" Making the Result Power?

Posted 9 years ago
2598 Views
|
2 Replies
|
0 Total Likes
|

Does anyone know why this is happening? When I append to a list using the function below Mathematica is "squaring" each of the elements in the list. When I change to using Append I get the following alternate output "Thread::tdlen: Objects of unequal length in {emptyList,{,,Subframe,-18.47,,5,D}} {emptyList} cannot be combined. >>"

See code below.

thanks

(* Test Function*)
myFunction[aLine_, sf_] := Module[{theList, theSF},
   theList = {"emptyList"};
   theSF = StringJoin[{"*"}, sf, {","}, {"D"}];
   If[StringMatchQ[aLine, theSF], AppendTo[theList, {aLine}]] (* 
    if aLine contains the sub-string append to the list *)
    theList
   ];

(* Test Case *)
testString = ",,Subframe,-18.47,,5,D";
theResult = myFunction[testString, "5"]
Head[theResult]
theResult[[1]]
Head[theResult[[1]]]

(* Unexpected Output *) 
{("emptyList")^2, {(",,Subframe,-18.47,,5,D")^2}}
List   
(emptyList)^2 
Power

(* Expected Ouput *)
{("emptyList"), {(",,Subframe,-18.47,,5,D")}}
POSTED BY: Bob Stephens
2 Replies
Posted 9 years ago

Excellent - that's it!! It would be nice if there were some sort of a notebook profiling tool that would allow the user to "single step" through lines in a cell to help detect this sort of an issue.

thanks for the quick (and accurate) response.

POSTED BY: Bob Stephens

The space between If[ ] and theList (where the comment is) gets interpreted as multiplication. Try adding a semicolon after the If[ ].

POSTED BY: Ilian Gachevski
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