How can I split an array into numerical and alphabetical arrays in Shopify?

Topic summary

A developer is attempting to split a Shopify array of clothing sizes (e.g., ‘6 - XXS, 8 - XS, 10 - S’) into two separate arrays: one containing numerical sizes and another containing letter sizes.

Goal: Compare a given size variable against both arrays, find a match, retrieve the forloop.index0, and display the corresponding value from the original availableSizes array.

Problem: The first and last Liquid filters aren’t working as expected, producing incorrect results like ‘6 - 5XL’ when printing numberSize and letterSize arrays.

Attempted Solutions:

  • Using split with ’ - ’ delimiter combined with first/last filters
  • Iterating through availableSizes with cycle to assign number/letter values, but unable to reassign these to separate arrays

The code snippet shows the attempted implementation using Liquid template syntax, but the filtering logic fails to properly separate numeric and alphabetic components. The discussion remains open with no resolution provided.

Summarized with AI on November 12. AI used: claude-sonnet-4-5-20250929.

I have an array of clothing sizes, which I need to divide into two array, where one will have a numbers and other one letters (numberSize and letterSize arrays), then I need to compare both of them to a given variable, and if there is a match, then get the forloop.index0 and show the same index variable from the main array availableSizes

And, well, I am stuck. The first and last filters don`t work the way I thought they would and I end up with 6 - 5XL if you print out {{ numberSize)) - {{ letterSize }}.

I also tried to iterate through availableSizes with cycle to assign number / letter, but couldn`t figure out how to re assign these values to a separate arrays

Hope you can help to figure this out

{%- assign availableSizes = '6 - XXS, 8 - XS, 10 - S, 12 - M, 14 - L, 16 - XL, 18 - XXL, 20 - XXL, 22 - 3XL, 24 - 4XL, 26 - 5XL' | split: ', ' -%}

{%- assign currentSize = 10 -%}
{%- assign numberSize = availableSizes  | split: ' - ' | first -%}
{%- assign letterSize = availableSizes | split: ' - ' | last -%}

{%- for potentialNumberSize in numberSize -%}
{%- if currentSize == potentialNumberSize -%}
{%- capture currentIndex %} {{ forloop.index0 }} {% endcapture %}
{%- endif -%}
{%- endfor -%}

{%- for potentialLetterSize in letterSize -%}
{%- if currentSize == potentialLetterSize -%}
{%- capture currentIndex %} {{ forloop.index0 }} {% endcapture %}
{%- endif -%}
{%- endfor -%}

["{{availableSizes[currentIndex]}}"]

// also tried this
// ["{% for potentialNumberSize in numberSize offset: currentIndex -%}{{- potentialNumberSize }}{% endfor %} - {% for potentialLetterSize in letterSize offset: currentIndex -%}{{- potentialLetterSize }}{% endfor %}"]
// and this
// ["{% for potentialNumberSize in numberSize[currentIndex] -%}{{- potentialNumberSize }}{% endfor %} - {% for potentialLetterSize in letterSize[currentIndex] -%}{{- potentialLetterSize }}{% endfor %}"]