-
Notifications
You must be signed in to change notification settings - Fork 0
/
amt4.pl
72 lines (65 loc) · 1.68 KB
/
amt4.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl
use strict;
use warnings;
sub Calculate_AMTS_TotalScore {
my @amts_items = (
'01. What is your age?',
'02. What is the year?',
'03. What is the name of this place (e.g. hospital)',
'04. What is your date of birth? (day and month sufficient)',
);
my %amts_scores;
# Get user input for each AMTS item
foreach my $item (@amts_items) {
my $valid_input = 0;
while (!$valid_input) {
print "Pleasem type score for: $item [0-1]: ";
my $score = <STDIN>;
chomp($score);
# Validate input
if ($score =~/^\d+$/ && $score >= 0 && $score <= 1) {
$amts_scores{$item} = $score;
$valid_input = 1;
}
else {
print "Invalid input. Please, type either 0 or 1. ";
}
}
}
# Calculate the total AMT score
my $total_score = 0;
foreach my $item_score (values %amts_scores) {
$total_score += $item_score;
}
return $total_score;
}
# Main Programme
print "Abbreviated Mental Test Score (AMTS) calculator\n";
print "===============================================\n";
my $total_amts_score = Calculate_AMTS_TotalScore();
# Print the total score of AMTS
print "\nThe total AMTS score is $total_amts_score\n";
while (1) {
print "Do you want to save the output to file (amt4.txt) [yes or no]? ";
my $ans = lc(<STDIN>);
chomp($ans);
if ($ans eq 'yes') {
my $output = 'amt4.txt';
open(FH,'>', $output) or die $!;
print FH
my $str = <<END;
The Abbreviated Mental Test Score (AMTS) is $total_amts_score
END
# print FH $str;
close(FH);
print "written to amt4.txt\n";
last;
}
elsif ($ans eq 'no') {
print "not saved\n";
last;
}
else {
print "out of range, please answer [yes or no] ";
}
}