Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.7K Views
|
4 Replies
|
1 Total Like
View groups...
Share
Share this post:

Function vs one line, What am I missing?

Posted 11 years ago

rose2 := {{z, 0, 1, 2, 3, 4}, {y, j, k, l, m, 5}, {x, i, b, c, n, 6}, {w, h, a, d, o, 7}, {v, g, f, e, p, 8}, {u, t, s, r, q, 9}};

roseEncode[str_] := Module[{strIn := str, toEn, cLst, rosePos}, toEn := StringCases[ToLowerCase[strIn], RegularExpression["[a-z,0,9]+"]];

cLst := Characters[StringJoin[toEn]];

rosePos := FirstPosition[rose2, #] &/@ cLst];

Return[Partition[rosePos, 2]]; ];

Print["Rose Encode: ", roseEncode["safe"]];

bark := FirstPosition[rose2, #] - 1 & /@ {s, t, a, r, t};

Print["Line Encode: ", bark]

Output: Rose Encode: {{Missing[NotFound],Missing[NotFound]},{Missing[NotFound],Missing[NotFound]}} Line Encode: {{5,2},{5,1},{3,2},{5,3},{5,1}}

The one liner works but the function doesn't. What am I missing? I have printed out cLst in the function and it is {s,t,a,r,t} just like in the one liner, I've tried replacing /@ with Map[rose2,cLst] with the same result. I also tried to Debug but have not figured out how to step through the Map and see each variable. Any help would be appreciated.

Mark

Attachments:
POSTED BY: Mark Henwood
4 Replies
Posted 11 years ago

Thanks Eric, You were right on the money. When I quoted the characters it worked. Thanks again for the help. Mark

POSTED BY: Mark Henwood
Posted 11 years ago
POSTED BY: Eric Rimbey
Posted 11 years ago

Mark,

In your attached notebook, rose2 includes Integers and Symbols, but no Strings. Your roseEncode function creates a string pattern to look for. Your one-liner, bark, is looking for Symbols, and it finds them.

Also, I'd suggest you check out the documentation for Set and SetDelayed. You are excessively using SetDelayed, and this is complicating your code. Your Module-based definition of roseEncode is also excessively complex. The one-liner you have that works is very easily translated into a simple function without the need to introduce Module.

POSTED BY: Eric Rimbey
Posted 11 years ago

Rose2 is a definition, a 2 dim array of scrambled alpha-numerics. The roseEn takes a string, throws away spaces etc and passes the results to map. It is called in the print statement and passed the string "start". If cLst is printed out during the function it is {s,t,a,r,t} the same as is passed into the one liner. I will look into the assignment differences, I've tried = before and kept getting errors so rather than fighting with it all the time. Mark

POSTED BY: Mark Henwood
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard