Need to extract from array string and print it in commentary window

I have this little formula example where i am trying to print the values from T_string2 but i dont see it.
How can i fix the code?

T_string2=",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA,";
 for( item = -1; ( sym = StrExtract( T_string2, item ) ) != ""; item-- ) 
        {
  printf( "here is the value sym%g"  , sym );
}

@suresh, just a guess, but I don't think you can use a "-1" as your item value. That would be asking for something before the first element.

Also not sure if the string will be Zero index or One index. But suggest you try both of those as your Initial item value and see what you get.

The problem is that i had a comma at the end of the string. Once i removed it, it worked.
for example
T_string2=",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA";
is valid for StrExtract
But
T_string2=",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA,";
is not valid

@snoopy.pa30 starting with -1 and then decrementing (item--) is fine.
it just starts at the end of the list and proceeds to 0th element, ie. first one. Its useful for reverse traversing.

From the above usage of StrExtract() there is nothing wrong, you just have to see what condition you have given to terminate the for loop.
In your code, you are telling AB to stop when an empty string is reached.
Since you have the last element followed by a comma, the first sub-string is therefore empty and stops the For Loop.

You should use %s as string format specifier instead of %g for sym.

1 Like

If you have comma at the end then either remove comma via StrTrim or start at -2 instead of -1 (since you iterate backwards).

Why does it not show results if there are comma at start?
Because you have said so via this part of loop statement

( sym = StrExtract( T_string2, item ) ) != "";

So it iterates until there is empty string. And since empty substring is at start already it stops there and you have empty output entirely.

So change to either like this

T_string2 = ",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA,";

for( item = -2; ( sym = StrExtract( T_string2, item ) ) != ""; item-- )
{
    printf( "here is the value sym %s\n", sym );
}

or to this one

T_string2 = ",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA,";

str_trim = StrTrim(T_string2, ",");

for( item = -1; ( sym = StrExtract( str_trim, item ) ) != ""; item-- )
{
    printf( "here is the value sym %s\n", sym );
}

Also you had incorrect format specifier.
%g is for number output
For string output you have to use %s.

If all is fixed then you will get

9


Also you may do like so


T_string2 = ",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA,";

length = StrCount(T_string2, ",");

for( item = length; item > -1; item-- )
{
    printf( "here is the value sym %s\n", StrExtract(T_string2, item) );
}

Then it does not stop and prints empty string
1

or like so to ignore empty


T_string2 = ",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA,";
T_string2 = StrTrim(T_string2, ",");

length = StrCount(T_string2, ",");

for( item = length; item > -1; item-- )
{
    printf( "here is the value sym %s\n", StrExtract(T_string2, item) );
}

2

Or iterating forward and setting item negative in StrExtract.

T_string2 = ",FFEF,FFFF,FAIA,EFEF,FAAE,ES,FAAI,OFAA,FFAO,FAAF,EAIA,";
T_string2 = StrTrim(T_string2, ",");

length = StrCount(T_string2, ",")+1;

for( item = 1; item <= length; item++ )
{
    printf( "here is the value sym %s\n", StrExtract(T_string2, -item) );
}
7 Likes