jed-users mailing list

[2004 Date Index] [2004 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: Dynamic string arrays in slang/jed?


On Wed, Feb 18, 2004 at 11:10:57AM -0500, Brian Blais wrote:
> Hello,
> 
> I'm new to this list, and to jed, but I searched the archives and the
> documentation and haven't found the answer to it.  Hopefully I am not
> asking something that has been hashed over before.
Yes it has  - see the thread "Howto concat arrays" in July 2003.

> Something like (which is not proper syntax):
> 
> fnames=[fnames; newstring];  % to add an element to the fnames string
The proper syntax would be

fnames=[fnames, newstring];

However:
> >Is it possible, that [a1,a2] returns a 2-D-array, if length(a1) ==
> >length(a2)?
> 
> Versions of slang _prior_ to 1.4.6 allowed one to create 2-d arrays in
> this manner if the dimensions of the arrays allow it, otherwise the
> arrays were concatenated.  The idea was that, e.g.,
> 
>    [[1,2,3],
>     [4,5,6]]
> 
> would be a convenient way to produce a 2x3 array.  Since there is an
> ambiguity in this approach, versions of slang _after_ 1.4.6 simply
> concatenate the arrays to produce a 1-d array.   

So it would seem that [array, element] is OK. However, in SLang 1.4.4

[a,b]

returns a 2 * 2 array if a is an array of length one, and b is not an
array but, say, a string.

However this can be overcome by writing [[a],b].  However this is even
slower than [a,b] which is itself not very fast.  It's faster to use a
comma-separated list with string concatenation instead of an array (or
in this case newline-separated, you could insert it in a buffer with a
single insert).  Even faster is to push the strings on the stack and
THEN concatenate them with create_delimited_string.

> 
> s=listdir(dirname);
> 
> for (i=0; i<length(s); i++) {
>     if (isimage(s[i])) {
>        insert(dirname+"/"+s[i]+"\n");
>     }
> }
The best way for this would be

s = s[where(array_map(Integer_Type, &isimage, s))];

But that's a different subject.

> so a general way to increase the size of an existing string array would
> be great.

To summarize, the general way is

[array, element]

unless someone is still using SLang 1.4.4.  It's faster if you allocate
the array in chunks:

#v+
define test2()
{
   variable a= ["foo", "bar"];
   tic();
   loop(1000)
     a = [a, "foo"];
   vinsert("\n%%took me %S seconds", toc());
}
%took me 1.45 seconds

define test5()
{
   tic;
   variable a = String_Type[100];
   variable i = 0, len = 100;
   loop(1000)
     {
	if (i >= len)
	  {
	     a = [a, String_Type[50]];
	     len += 50;
	  }
	a[i] = "foo";
	i++;
     }
   vinsert("\n%%took me %S seconds", toc());
}
%took me 0.05 seconds
#v-
Which is even faster than a list, and works in SLang 1.4.4 as well.


--------------------------
To unsubscribe send email to <jed-users-request@xxxxxxxxxxx> with
the word "unsubscribe" in the message body.
Need help? Email <jed-users-owner@xxxxxxxxxxx>.


[2004 date index] [2004 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]