Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Numeric value populating external .htm graphics file?

I have a numeric value in my study and it would be great if I could dynamically plug that value into an external .htm file in my graphics folder. Does such a capability exist?
asked Feb 15 by WTW_cal Bronze (1,375 points)

1 Answer

0 votes
Modifying an external file is possible using Perl.  I placed a test.txt file in my graphics folder and was able to successfully write to it by placing this script in my survey:

[% Begin Unverified Perl
my $file = '../../graphics/test.txt';
open (my $fh, '>', $file) or die 'File could not be opened';
print $fh 'Hello world!';
close $fh;
End Unverified %]


(The above code should work for local testing.  For the live survey, remove one of the "../" from line 2.)

So it is certainly possible to modify your .htm file using this technique.  The exact code needed will depend on exactly how you need the .htm modified.
answered Feb 15 by Zachary Platinum Sawtooth Software, Inc. (89,975 points)
Excellent. I would like this code to place content in the .htm file at a very specific location, within some html code. Can this method be adapted to accommodate? Your note suggests yes. Maybe I can follow-up with you directly?
Hopefully this script will get you going:

[% Begin Unverified Perl
my $in = '../../graphics/in.txt';
my $out = '../../graphics/out.txt';

open (my $reader, '<', $in) or die;
open (my $writer, '>', $out) or die;

while (my $line = <$reader>) {
    chomp $line;
    my $search = 'abc';
    my $replace = 'xyz';
    $line =~ s/$search/$replace/g;
    print $writer $line;
}

close $reader;
close $writer;
End Unverified %]


This code reads in in.txt, replaces every "abc" with "xyz," then prints out the result to out.txt.  Hopefully you can use this to dynamically make copies of your .htm with your needed adjustments.
This works, but there are two things working against me.

1) I want the $replace to be [%q%]
2) As I want it to be dynamic for every respondent I'd have to make the $out also be unique per respondent otherwise people will see the wrong information, resulting in 1,000s of unique files
You could potentially delete the file at a later point in the survey, like on the terminate pages?
...