Author Topic: Invalid expression creating initialized string array  (Read 300 times)

rowbearto

  • Senior Community Member
  • Posts: 938
  • Hero Points: 43
Invalid expression creating initialized string array
« on: February 05, 2018, 03:22:24 am »
I'm trying to create a macro with an initialized string array.

I put this in my Slick-C code:

Code: [Select]
_str    gastring1[]={"Value1", "Value2"};
When I load(F12) the macro, this line yields "Invalid expression".

I got this example from the SE help. Help->Index, then in "Look for:", I put "Arrays". Double click on "Arrays", and I find this example of "_str    gastring1[]={"Value1", "Value2"};".

What am I doing wrong?
« Last Edit: February 05, 2018, 04:05:03 am by rowbearto »

Graeme

  • Senior Community Member
  • Posts: 2091
  • Hero Points: 252
Re: Invalid expression creating initialized string array
« Reply #1 on: February 05, 2018, 09:12:31 am »
I get no error for that line.  Can you post the whole file  - or try it in a small file of its own.  Or try a new empty config folder.


rowbearto

  • Senior Community Member
  • Posts: 938
  • Hero Points: 43
Re: Invalid expression creating initialized string array
« Reply #2 on: February 05, 2018, 02:03:24 pm »
Thanks for taking a look Graeme.

Attached is my entire .e file where I get the error when trying to Load(F12) it.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2053
  • Hero Points: 98
Re: Invalid expression creating initialized string array
« Reply #3 on: February 05, 2018, 02:21:02 pm »
You can't initialize a local variable like that.  It will work for a global (which is what I think it probably is in the example).  There's a reason for this but I forget exactly what it is.

For a local array, the best thing is probably to use ARRAY_APPEND.

Code: [Select]
   _str gastring1[];
   ARRAY_APPEND(gastring1,"Value1");
   ARRAY_APPEND(gastring1,"Value2");
   _dump_var(gastring1);

You could also just use the :+= operator that ARRAY_APPEND uses (you can look at the definition of ARRAY_APPEND in macros/slick.sh.

Dennis

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2332
  • Hero Points: 308
Re: Invalid expression creating initialized string array
« Reply #4 on: February 05, 2018, 04:58:19 pm »
Instead of ARRAY_APPEND(), you can also use Slick-C's builtin :+= (append to) operator.  :+= works for both strings and arrays.

Code: [Select]
_str gastring1[];
gastring1 :+= "Value1";
gastring1 :+= "Value2";
_dump_var(gastring1);

mstring := "";
mstring :+= gastring[0];
mstring :+= " ";
mstring :+= gastring[1];
say("mstring="mstring);

rowbearto

  • Senior Community Member
  • Posts: 938
  • Hero Points: 43
Re: Invalid expression creating initialized string array
« Reply #5 on: February 05, 2018, 05:45:31 pm »
Dan, Dennis: Thanks for explaining this and giving the workarounds.

Would be nice to have a more meaningful error message in this case - one that would let me know that can't declare this as a local.