7
$\begingroup$

I want to replace plus by comma and the order of numbers in the list should be in increasing order. Example:

a1 + a10 + a11 + a13 + a2 + a27 + a28

In this expression, I want to replace plus sign by comma and delete "a". To get a list of numbers and the order should be

{1,2,10,11,13,27,28}

I have tried this, but it's not convenient...

StringReplace["a1 + a 10 + a 11 + a 13 + a2 + a27 + a28" , "+ a" -> ","]
$\endgroup$

8 Answers 8

6
$\begingroup$

Another way:

expr = a1 + a10 + a11 + a13 + a2 + a27 + a28;

ToExpression@StringDrop[ToString[#], 1] & /@ List @@ expr

(* ==> {1, 10, 11, 13, 2, 27, 28} *)

You can then Sort them.

$\endgroup$
0
6
$\begingroup$

Another option:

Block[
    {Plus = Function[Sort@ToExpression@StringReplace[ToString[{##}], LetterCharacter -> ""]]},
    a1 + a10 + a11 + a13 + a2 + a27 + a28
]
(* {1, 2, 10, 11, 13, 27, 28} *)

Works with other possibilities, such as "b123", "abc123", etc.

$\endgroup$
5
$\begingroup$
expr = a1 + a10 + a11 + a13 + a2 + a27 + a28

Then:

expr /. Plus -> List // ToString /@ # & // StringReplace[#, "a" -> ""] & // ToExpression // Sort

Gives:

{1, 2, 10, 11, 13, 27, 28}

$\endgroup$
5
$\begingroup$

Yet another way:

Sort @ ToExpression @
  StringCases[ToString[a1 + a10 + a11 + a13 + a2 + a27 + a28], 
              DigitCharacter ..]
$\endgroup$
3
  • $\begingroup$ NumberString will do too. +1. $\endgroup$
    – Kuba
    Jan 23, 2014 at 23:41
  • $\begingroup$ I just realized that my answer duplicates yours. I hope my use of DigitQ and FromDigits is sufficient to justify the existence. If you disagree I'll delete mine and add that variant to yours. $\endgroup$
    – Mr.Wizard
    Jan 25, 2014 at 0:57
  • $\begingroup$ @Mr.Wizard I don't have a problem with it. Thanks for asking. $\endgroup$
    – Michael E2
    Jan 25, 2014 at 1:45
3
$\begingroup$

The following employs only 3 functions. (I assume the input is a string.)

Sort@ToExpression@StringSplit[t, {" + a", "a"}]
$\endgroup$
2
$\begingroup$

Another formulation:

expr = a1 + a10 + a11 + a13 + a2 + a27 + a28;

FromDigits /@ StringCases[ToString @ expr, __?DigitQ] // Sort
{1, 2, 10, 11, 13, 27, 28}
$\endgroup$
0
2
$\begingroup$

Inspired by @R.M.'s answer, here is another way to temporarily define Plus:

Block[{Plus = Sort[FromDigits@StringDrop[SymbolName@#, 1] & /@ {##}] &}, expr]

{1, 2, 10, 11, 13, 27, 28}

$\endgroup$
1
$\begingroup$

Just a thought here because the question has already been answered well. But If you had the option of generating that equation then your variables are named in an awkward way. It would be much easier if you had used subscripts.

$eqn= a_1+a_{10}+a_{11}+a_{13}+a_2+a_{27}+a_{28} $

Then you could have done something like this:

List @@ eqn /. Subscript[a, c_] -> c
$\endgroup$
1
  • 2
    $\begingroup$ There are other options such as down-values as well... however, using subscripts opens a whole new can of worms, especially with assigning definitions and clearing them :) $\endgroup$
    – rm -rf
    Jan 26, 2014 at 23:59

Your Answer

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

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