MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Got a tip ?

I presume your arguments are strings (and not symbols). Also, I assume
that you wish ultimately to return a list of numbers (not strings).
So, for example, f["a"] gives {1} (not {"1"}) and f["za"] gives {26,1}
(not {"26","1"}), in which case, the following might be what you are

 {rules = Thread[CharacterRange["a", "z"] -> Range[26]]},
 f[input_String] := List @@ StringReplace[input, rules]

The "With" and "rules =" are to avoid having to evaluate the rules
every time you call the function. If you look at Information[f] after
having evaluated my line of code, you will see what I mean. The main
part of the function is a string replace, which converts all letters
between "a" and "z" to the integers 1 through 26. Note that
StringReplace will return either a String or a StringExpression, so
you need to change the head to List if that is what you ultimately
want (this only works if there really was at least one letter from "a"
to "z" in the input). Note that capitals will not get transformed. You
can change the StringReplace to be StringReplace
[input,rules,IgnoreCase->True] if you want to include capitals. Any
other characters will just be returned in the list as strings. You can
add rules to cover them if you wish. For example, if you wanted all
other characters to be returned as the integer 0, then you could
define the function as:

 { rules = Join[ Thread[CharacterRange["a", "z"] -> Range[26]] , {_-
>0} ]  },
 f[input_String] := List @@ StringReplace[input, rules,IgnoreCase-

Hope that helps,


  • Prev by Date: Re: Question about how to graph the vector field for the differential
  • Next by Date: FW: Problem using a dialog and dynamic - problem sorted
  • Previous by thread: Re: Got a tip ?
  • Next by thread: Help x-y pairs with rules