Jump to: main text

Associative-array examples from Inline::SLang

The following code and examples can be found in the Inline::SLang distribution available from CPAN. The output was created using version 1.00 of the module, using the PDL support, together with version 1.4.9 of the S-Lang library.

S-Lang associative arrays are converted to hash references in Perl, as shown below. The datatypes example page may also be useful. The support for associative arrays is described in the Inline::SLang::Assoc documentation that comes with the module.

A simple example

use Inline 'SLang';

# you can send hash references to S-Lang
print_in_slang( { a => 23, "b b" => "foo" } );

# and get them back from S-Lang
$href = get_from_slang();
print "The assoc array contains:\n" .
  join( "", map { "\t$_ => $$href{$_}\n"; } keys %$href );


define print_in_slang (assoc) {
  message( "SLang thinks you sent it an assoc. array with:" );
  foreach ( assoc ) using ( "keys", "values" ) {
    variable k, v;
    ( k, v ) = ();
    vmessage( " key %s = %S", k, v );
define get_from_slang() {
  variable x = Assoc_Type [String_Type];
  x["a key"] = "a value";
  x["another key"] = "another value";
  return x;

which, when run, produces

SLang thinks you sent it an assoc. array with:
 key a = 23
 key b b = foo
The assoc array contains:
	another key => another value
	a key => a value