2

I currently have the following problem for which I couldn't find a solution for. It's about getting a substring from a string variable...

  1. I've a nested list of values:

    \ExplSyntaxOn
        \NewDocumentCommand{\DeclareListOfValues}{ m m }
        {
            \clist_gclear_new:c {g_list_of_values_#1_clist}
            \clist_gset:cn {g_list_of_values_#1_clist} {#2}
        }
    
        \NewDocumentCommand{\GetValueFromList}{ m m }
        {
            \clist_item:cn {g_list_of_values_#1_clist} {#2}
        }
    \ExplSyntaxOff
    
    %List structure: {TotResClassA,TotResClassB,TotResClassC,TotResClassD,TotResClassE}
    \DeclareListOfValues{GraphiteArrangementClassesTotalRes}{70.2,29.8,0,0,0}
    %List structure: {{ImgName,ResClassA,ResClassB,ResClassC,ResClassD,ResClassE},{...},{...},...}
    \DeclareListOfValues{GraphiteArrangementClasses}{
        {14-1-11,73.6,26.4,0,0,0}, 
        {15-1-4,72.7,27.3,0,0,0}, 
        {15-1-6,67.7, 32.3,0,0,0},
        {16-1-10,75.6, 24.4,0,0,0},
        {1562-3-6,68.7,31.3,0,0,0},
        {1562-3-16,71.6,28.4,0,0,0},
        {1563-3-8,85.9,14.1,0,0,0},
        {1563-3-13,71.8,28.2,0,0,0},
        {1564-2-7,58.1,41.9,0,0,0},
        {1564-3-14,56.2,43.8,0,0,0}
    }
    
  2. Then I get the 1st element in the list, like shown below:

    \newcommand{\teststr}{\GetValueFromList{GraphiteArrangementClasses}{1}} %teststr: 14-1-11,73.6,26.4,0,0,0 
    

What I need to get now from this string is the name of the image, which ist 14-1-11. I tried it with xstring, but it didn't work. It only works, if teststring is some arbetrary string of comma-separated characters, like

    \newcommand{\teststr}{123,2563,58888,9541278}
    \StrBefore[1]{\teststr}{,} %res: 123

but not if I get the string from a list, like in my case - and I cannot understand, why and what's actually the difference!?

Would be very glad, if somebody could help me solving this!

Best regards, Michael

1 Answer 1

1

I guess you not only want to retrieve one item, but any item inside.

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\DeclareListOfValues}{ m m }
  {
    \clist_gclear_new:c {g_list_of_values_#1_clist}
    \clist_gset:cn {g_list_of_values_#1_clist} {#2}
  }

\NewExpandableDocumentCommand{\GetValueFromList}{ m o m }
  {
    \IfValueTF { #2 }
      {
        \exp_args:Ne \clist_item:nn { \clist_item:cn {g_list_of_values_#1_clist} {#3} } { #2 }
      }
      {
        \clist_item:cn {g_list_of_values_#1_clist} {#3}
      }
  }
\ExplSyntaxOff

%List structure: {TotResClassA,TotResClassB,TotResClassC,TotResClassD,TotResClassE}
\DeclareListOfValues{GraphiteArrangementClassesTotalRes}{70.2,29.8,0,0,0}
%List structure: {{ImgName,ResClassA,ResClassB,ResClassC,ResClassD,ResClassE},{...},{...},...}
\DeclareListOfValues{GraphiteArrangementClasses}{
    {14-1-11,73.6,26.4,0,0,0}, 
    {15-1-4,72.7,27.3,0,0,0}, 
    {15-1-6,67.7, 32.3,0,0,0},
    {16-1-10,75.6, 24.4,0,0,0},
    {1562-3-6,68.7,31.3,0,0,0},
    {1562-3-16,71.6,28.4,0,0,0},
    {1563-3-8,85.9,14.1,0,0,0},
    {1563-3-13,71.8,28.2,0,0,0},
    {1564-2-7,58.1,41.9,0,0,0},
    {1564-3-14,56.2,43.8,0,0,0}
}

\begin{document}

\GetValueFromList{GraphiteArrangementClasses}{1}

\GetValueFromList{GraphiteArrangementClasses}[1]{1}

\GetValueFromList{GraphiteArrangementClasses}[2]{1}

\GetValueFromList{GraphiteArrangementClasses}{2}

\GetValueFromList{GraphiteArrangementClasses}[1]{2}

\GetValueFromList{GraphiteArrangementClasses}[2]{2}

\end{document}

enter image description here

1
  • Thank you very much)) Commented Feb 23, 2023 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .