slang-users mailing list

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

Re: [slang-users] Preserving order in an associative array


Hi Morten,

I don't think you can use associative arrays for your purpose.
I believe that the order of the keys in a hash is undefined in other 
programming languages as well. Consider, e.g., Perl:

   my %S;
   $S{"b"} = "c";
   $S{"d"} = "e";
   $S{"a"} = "b";
   my @a;
   foreach my $k (keys %S) {
     push(@a, $k . $S{$k});
   }
   print( join("\n", @a) );

(note that I had to change the input to non-alphabetical order) gives me

   ab
   bc
   de


But as a solution for S-Lang, I'd suggest to use data types which have a 
defined ordering, i.e., arrays, or even better: lists.

   variable S = {};
   list_append(S, struct { key="a", value="b" });
   list_append(S, struct { key="b", value="d" });
   list_append(S, struct { key="d", value="e" });

   variable k_v, a = {};
   foreach k_v (S)
     list_append(a, strcat(k_v.key, k_v.value));

   variable file = strjoin(list_to_array(a), "\n");


Note that the `a = [a, s];' statement you had in your loop works, but 
becomes very slow as `a' becomes larger. I'd chose a list as in the 
above example, if I don't know how long `a' is going to become (and 
convert it only at the end back to an array).
In this example, we know what `length(a)' will be before we construct 
it, so we can just create the array from the beginning.
In addition, one can avoid the overhead of many structures with the same 
fields when using separate lists for "keys" and "values":

   variable key={}, value={};
   list_append(key, "a"); list_append(value, "b");
   list_append(key, "b"); list_append(value, "d");
   list_append(key, "d"); list_append(value, "e");

   variable i, n = length(key);
   variable a = String_Type[n];
   _for i (0, n-1, 1)
     a[i] = strcat(key[i], value[i]);

   variable file = strjoin(a, "\n");


Cheers and happy S-Lang scripting, :-)

Manfred

--
Manfred Hanke      Manfred.Hanke(at)sternwarte.uni-erlangen.de
Dr. Karl Remeis-Observatory,  University of Erlangen-Nuremberg
Sternwartstrasse 7,      96049 Bamberg, Germany
phone: ++49 951 95222-34                fax: ++49 951 95222-22
   web: http://pulsar.sternwarte.uni-erlangen.de/hanke
--


Morten Bo Johansen wrote:
> Hi,
>
> It seemed to me that it could be useful to split a file into a number of
> strings and then split these strings into an associative array, do something
> on one of these strings based on its linkage to the other and then finally
> assemble it all back into the file. But if, just to illustrate, I do;
> 
>    variable S = Assoc_Type[String_Type];
>    variable k, v, s, file;
>    variable a = String_Type[0];
>    
>    S["a"] = "b";
>    S["b"] = "c";
>    S["d"] = "e";
> 
>    foreach k, v (S) using ("keys", "values")
>      {  
>         s = strcat (k, v);
>         a = [a, s];
>      }
>    
>    file = strjoin (a, "\n");
> 
> and I output this, I get;
> 
>   ab
>   de
>   bc
> 
> i.e. the order in which I entered the pairs is not preserved. Please
> note, that I do not want any kind of sorting, I just want to preserve the
> original order in which the keys and values were entered into the array.
> 
> 
> Thanks,
> 
> Morten



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