printing an array on one line

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

printing an array on one line

Ted Lewis
In a macro, I've been calculating statistics based on results from
'analyze particles', and I'm sending the results to the 'log' window
with a 'print' statement.

This is fine when the statistics are defined as variables, but for some
statistics (histogram bins & percentiles), I need to use arrays: the
number of bins and percentiles is user-defined.

Is there any way to print an array on *one* log-window line, if you
don't know in advance the length of the array?

Thank you.

--
Ted Lewis
PhD Candidate
Climate System Research Center
Department of Geosciences
University of Massachusetts at Amherst
413-545-0659
http://www.geo.umass.edu/gradstud/lewist/
Reply | Threaded
Open this post in threaded view
|

Re: printing an array on one line

Wayne Rasband
> In a macro, I've been calculating statistics based on results
> from  'analyze particles', and I'm sending the results to the
> 'log' window with a 'print' statement.
>
> This is fine when the statistics are defined as variables, but
> for some statistics (histogram bins & percentiles), I need to
> use arrays: the number of bins and percentiles is user-defined.
>
> Is there any way to print an array on *one* log-window line,
> if you don't know in advance the length of the array?

Use string concatenation. Here is an example:

    array = newArray(103.3, 84.3, 135.5, 85.0, 175.1);
    str = "";
    for (i=0; i<array.length; i++)
        str = str + array[i] + " ";
    print(str);

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: printing an array on one line

Mark R. Besonen
In reply to this post by Ted Lewis
At 01:56 PM 7/22/2005, you wrote:

>In a macro, I've been calculating statistics based on results from
>'analyze particles', and I'm sending the results to the 'log' window with
>a 'print' statement.
>
>This is fine when the statistics are defined as variables, but for some
>statistics (histogram bins & percentiles), I need to use arrays: the
>number of bins and percentiles is user-defined.
>
>Is there any way to print an array on *one* log-window line, if you don't
>know in advance the length of the array?


Hi Ted,

         The length of any array is a predefined/innate property that you
can simply query with the ".length" modifier.  For example, you've built an
array called "BIRD", it goes from BIRD[0] to BIRD[BIRD.length].  So you
really don't need an actual number for the array length, but if you really
want it you also just assign it to a variable, for example:

myarraylength=BIRD.length;

         To do what you want to do, you can make a loop that skips through
the array bin by bin, and concatenates all the bins together into a new
variable.  Then you can just print that new variable as a single line.

BIRD=newArray("1","56","6342","999");
mystring=BIRD[0];
for (i=1; i<BIRD.length; i++) {
         mystring=mystring+"-----"+BIRD[i];
         };
print(mystring);

Mark





>Thank you.
>
>--
>Ted Lewis
>PhD Candidate
>Climate System Research Center
>Department of Geosciences
>University of Massachusetts at Amherst
>413-545-0659
>http://www.geo.umass.edu/gradstud/lewist/