Passing lists using the Script Library

I got asked a question the other day - I have a list of solvents that I want to pass into a script for DMol3/COSMO calculation, how can I do this using the Script Library?

The Script Library enables you to design a simple interface that allows you to pass in variables such as integers, floating point numbers, boolean arguments and strings. However, in it's current form, you cannot select from a set of options such as, for example, a list of solvents. However, you can quite easily convert a string to a list or array in Perl if you have a common separator between the items in the list. In this example, I used a comma to denote the separator and the argument I am passing in is called List.

# Simple example to show how to pass a comma separate list into a script.

my %Args;

GetOptions(\%Args, "List=s");

my @list = split(",",\$Args{List});

foreach my \$l (@list) { print "\$l"."\n";}

The above script fragment works well if your user knows that they have to type in the options without any whitespace between the separator. However, if they enter "water, acetone" rather than "water,acetone" they will get an error as DMol3 will not understand the whitespace at the begining of " acetone". A better way to do this would be to remove the whitespaces at the beginning and end of each string. You can do this with the following script:

my %Args;

GetOptions(\%Args, "List=s");

#Use the grabList subroutine to create the list

my @cleanedList = grabList(\$Args{List});

foreach my \$l (@cleanedList) { print "\$l"."\n";}

# Gets the items in the string and splits them into a list


sub grabList {   
    my (\$listString) = @_;

    # Split the array based on the comma

    my @listArray = split(",",\$listString);

    my @cleaned = ();

    # Remove the start and end whitespace in the list

    foreach my \$l (@listArray) {

        \$l =~ s/^\s+|\s+\$//g;

        push (@cleaned, \$l);

    }   

    return @cleaned;

}

This script fragment now splits the string and also cleans the whitespace from the start and end of each item in the list.

The above script and .xml for the script library are attached.

Cheers

Stephen