DECLARE LIST OF STRING VARIABLES

Discussions about CSEntry
Forum rules
New release: CSPro 8.0
Post Reply
col Ar
Posts: 24
Joined: January 11th, 2019, 4:05 am

DECLARE LIST OF STRING VARIABLES

Post by col Ar »

Dear all,
I would like to declare a string array taking 60 characters
I have done the following:

Code: Select all

array string myString(100), myString2(100), myString3(100);
However, it fails to compile except when I declare only 1 string as:

Code: Select all

array string myString(100);
Can someone help please?
khurshid.arshad
Posts: 571
Joined: July 9th, 2012, 11:32 am
Location: Islamabad, Pakistan

Re: DECLARE LIST OF STRING VARIABLES

Post by khurshid.arshad »

your declaration maybe look like this:

array string(100) myString(3); {3 names, each up to 100 characters long}

example from help

Code: Select all

Example 2: (alphanumeric array)

PROC GLOBAL
  array alpha(10) crop (20); {20 crop names, each up to 10 chars long}

PROC MY_PROGRAM
preproc
  crop(1)= "maize";
  crop(2)= "wheat";
  crop(3)= "rice";
  crop(4)= "potatoes";

Best.
a.
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: DECLARE LIST OF STRING VARIABLES

Post by josh »

Just put each one a separate line:
array string myString(100);
array string myString2(100);
array string myString3(100);
Note that a string is variable length so it can have as many characters as you want. So

Code: Select all

array string myString(100)
is an array of 100 strings not a string of length 100.

With the old alpha type that you see in Khurshid's example you had to specify the length. But you should always use string rather than the alpha type. alpha is a leftover from early versions of CSPro that didn't have variable length strings.
col Ar
Posts: 24
Joined: January 11th, 2019, 4:05 am

Re: DECLARE LIST OF STRING VARIABLES

Post by col Ar »

That's so clear. Thanks for the clarification
col Ar
Posts: 24
Joined: January 11th, 2019, 4:05 am

Re: DECLARE LIST OF STRING VARIABLES

Post by col Ar »

So, how do I declare those 3 stings as I want them each to have a length of 100,not 100 strings as @Josh has explained. My aim is to put them in same line coz I have other strings that take different lengths
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: DECLARE LIST OF STRING VARIABLES

Post by josh »

The whole point of string variables is you do not need to specify the length. The system makes them as long you need them to be based on what values you assign to them. So you don't need to specify the length at all.
Gregory Martin
Posts: 1777
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: DECLARE LIST OF STRING VARIABLES

Post by Gregory Martin »

This, as an example, declares an array of 100 alpha objects where each is 60 characters:
array alpha(60) my_array_name(100);
As Josh mentioned, we suggest that you use strings instead of alphas. If you are concerned about how a report looks when written out, you can control the width of the strings when written out. For example:
write("%-60s", my_string_name);
That will pad the string so that it is left-justified and takes up 60 characters. See more here:

http://www.csprousers.org/help/CSPro/me ... tions.html
col Ar
Posts: 24
Joined: January 11th, 2019, 4:05 am

Re: DECLARE LIST OF STRING VARIABLES

Post by col Ar »

This is well understood
Post Reply