2
$\begingroup$

Hello stackexchange community!

I have a stringlist with a value and his error for eg:

{{"12","1"},{"13","2"}}

Now i want to output this as something like this (for a lab-report):

{"12(1)","13(2)"}

Unfortunately I have no idea how to get this working...

Thanks in advance
Cx

$\endgroup$
  • $\begingroup$ Your first list as pasted does not contain strings, but integers. Could you clarify? $\endgroup$ – Yves Klett Nov 11 '13 at 13:09
  • $\begingroup$ sorry, i corrected the error now $\endgroup$ – cxkoda Nov 11 '13 at 13:21
  • $\begingroup$ now I solved it like this: errformat[list_] := Table[StringJoin[ToString[list[[i, 1]]], "(", ToString[list[[i, 2]]], ")"], {i, 1, Dimensions[list][[1]]}] $\endgroup$ – cxkoda Nov 11 '13 at 13:32
  • $\begingroup$ I would recommend Chris` solution, which is clean&instructive. BTW, try Length[list]. $\endgroup$ – Yves Klett Nov 11 '13 at 13:35
4
$\begingroup$

Or with a pattern:

{{"12", "1"}, {"13", "2"}} /. {a_String, b_String} :> StringJoin[a, "(", b, ")"]
$\endgroup$
3
$\begingroup$

Using Apply:

StringJoin[#1, "(", #2, ")"] & @@@ {{"12", "1"}, {"13", "2"}}

{"12(1)", "13(2)"}

$\endgroup$
  • $\begingroup$ Cleaner than mine :-) $\endgroup$ – Yves Klett Nov 11 '13 at 13:34
3
$\begingroup$

Depending on what you intend to do with the output you may also use StringForm:

StringForm["``(``)", ##] & @@@ {{"12", "1"}, {"13", "2"}}

{12(1),13(2)}

Internally this remains as a StringForm expression which may be good or bad given the circumstance.

$\endgroup$
1
$\begingroup$

Depending on your exact input/output you could start with this and modifiy it according to your wishes:

ToString[#[[1]]] <> "(" <> ToString[#[[2]]] <> ")" & /@ {{12,1}, {13, 2}}

{"12(1)", "13(2)"}

Edit: with all elements as strings, you can dump ToString:

#[[1]] <> "(" <> #[[2]] <> ")" & /@ {{"12", "1"}, {"13", "2"}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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