2
$\begingroup$

I have a list genus, containing the list of genus group names for some fishes I work on derived from a column of imported data:

genus = tempData2[[All,2]] (* here explicitly specifying just the 4 of them for brevity*)

returning the list

{Synanciea, Pterois, Helicolenus, Sebastes}

If I examine the head using

Head[genus]

it returns List as expected

If I examine the contents of the list using

Table[Head[genus[[i]], {i, 1, Length[genus]}] (*fudging here that only 4 are used*)

it returns, as expected

{String, String, String, String}

I wish to simply take the individual strings and append a " " to each string as follow

genusname = Map[ReplaceAll[#&, #-> #<>" "],genus]

This works as intended returning appending the blank character onto each original string in the list:

{Synancia , Pterois , Helicolenus , Sebastes }

or {"Synancia ","Pterois ", "Helicolenus ", "Sebastes "} (when //InputForm is used in postfix notation)

However, it also generates the Error ... "StringJoin: String expected at position 1 in #1"

If I try to explicitly convert the slot to String as follows

Map[ReplaceAll[# &, ToString[#]-> ToString[#]<>" "], genus]

it returns no error, but it doesn't append the blank character at the end of each string in the list.

Obviously, I can suppress/ignore the error since the first effort works, but I am not sure why it occurs.
Can anyone explain why the first effort at Map returns the error but works, whereas the second effort of Map does not return an error but does not work (leaves each element of the list unchanged)?

$\endgroup$

3 Answers 3

5
$\begingroup$

First, I think you have a typo in your question. This:

genusname = Map[ReplaceAll, # &, # -> # <> " "], genus]

is not valid Wolfram Language syntax, you probably meant to write:

genusname = Map[ReplaceAll[# &, # -> # <> " "], genus]

If you do this in a notebook interface you will see that some of the slots (#) are purple, indicating that they are not bound to a Function.

You can sort of fix that by placing the Function shorthand operator (&) at the end:

genusname = Map[ReplaceAll[#, # -> # <> " "] &, genus]

which makes all the #'s green (green is good!) and gives you the answer you want.

But a simpler way it to use this:

Map[# <> " " &, genus]

which takes each string in turn and uses StringJoin (<>) to add a space character.

$\endgroup$
3
  • 1
    $\begingroup$ Green is always good. Eventually, I'll figure out anonymous functions. Thanks. $\endgroup$ Apr 17, 2020 at 22:08
  • 1
    $\begingroup$ Typos corrected in original post. $\endgroup$ Apr 17, 2020 at 22:20
  • $\begingroup$ You can always write anonymous functions with, for example Function[#^2]. That way you don't have to worry about where to put the & and if it needs parenthetical grouping. And the code becomes slightly more readable since it isn't a big ASCII soup of characters. $\endgroup$ Apr 17, 2020 at 23:44
5
$\begingroup$

I wish to simply take the individual strings and append a " " to each string

One possibility is to use StringInsert

  list = {"Synanciea", "Pterois", "Helicolenus", "Sebastes"};
  StringInsert[#, " ", -1] & /@ list

gives

  {"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}
$\endgroup$
3
$\begingroup$
ReplaceAll[genus, s_String :> s <> " "] (* or *)

ReplaceAll[s_String :> s <> " "] @ genus
 {"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}

Also

StringReplace[EndOfString -> " "] @ genus
{"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}
StringReplace[genus, s__ :> s <> " "] 
{"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}
StringPadRight[#, 1 + StringLength@#] & /@ genus
{"Synanciea ", "Pterois ", "Helicolenus ", "Sebastes "}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.