I am glad you enjoyed it. Perl coding is fun. I do have a couple of suggestions
suggestion 1: use strict and warnings. These will alert you to dangerous coding practices. They are a great way to learn to write solid code.
#!/usr/bin/perl
use strict;
use warnings;
:-)
OK here is a version of your code tidied up a bit, compiling under strict and not throwing warnings
#!/usr/bin/perl
use strict;
use warnings;
# switch one in for testing each case.
#
# my $statement = "File extraction sucessfully completed in 8s 180ms";
my $statement = "File extraction sucessfully completed in 3min 8s";
# my $statement = "File extraction sucessfully completed in too much t
+ime";
my @values = split(/\s+/, $statement); # split on any number/sort of w
+hitespace
my @time;
$time[0] = pop @values;
$time[1] = pop @values;
# or use and array slice
# my @time = @values[-2,-1];
my $seconds = 0;
for my $val (@time) {
if ($val =~ s/min$//) {
$seconds = $seconds + $val * 60;
}
elsif ($val =~ s/ms$//) {
$seconds = $seconds + $val / 1000;
}
elsif ($val =~ s/s$//) {
$seconds = $seconds + $val;
}
else {
print "Warning, strange value in expected time field: $val\n";
exit;
}
}
print "Total time taken: $seconds seconds \n"
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
|