Is there any other way to access the n-th element of a BlankSequence x__?
You can also use Slot
(that is, #n & @ x
):
Cases[{x__, 1} :> ( #4 & @ x)][
{{a, 2}, {a, b, c, d, e, 1}, {x, y, z, w, 1}, {a, b, c, 1}}]
and Indexed
(that is, Indexed[{x}, n]
):
Cases[{x__, 1} :> Indexed[{x}, 4]][
{{a, 2}, {a, b, c, d, e, 1}, {x, y, z, w, 1}, {a, b, c, 1}}]
compare with Part
:
Cases[{x__, 1} :> {x}[[4]]][
{{a, 2}, {a, b, c, d, e, 1}, {x, y, z, w, 1}, {a, b, c, 1}}]
You can use Repeated
instead of BlankSequence
to match the fourth element directly. Using the example from kglr:
Cases[{Repeated[_, {3}], x_, ___, 1} :> x][
{{a, 2}, {a, b, c, d, e, 1}, {x, y, z, w, 1}, {a, b, c, 1}}]
{d, w}
Are you looking for this sort of result?
{{a, 2}, {a, b, c, d, e, 1}, {x, y, z, w, 1},
{a, b, c, 1}} /. {x__, 1} :> Last[{x}]
(* Out: {{a, 2}, e, w, c} *)
Or:
{{a, 2}, {a, b, c, d, e, 1}, {x, y, z, w, 1},
{a, b, c, 1}} /. {x__, 1} :> Take[{x}, {Length[{x} - 1]}]
(* Out: {{a, 2}, {e}, {w}, {c}} *)