in reply to Time transformation
You should probably do a loop over both $xcltime and $xcltime2, to make sure BOTH of them get the treatment.my $xcltime = "19:45:34"; # It should print 7:45 PM my $xcltime2 = "23:35:14"; # It should print 11:35 PM if (($xcltime =~ /(\d{1,2}):(\d{1,2}):(\d{1,2})/) || ($xcltime2 =~ /(\ +d{1,2}):(\d{1,2}):(\d{1,2})/)) {
You're confusing Perl's "OR" operator || with the regex's alternation symbol |. When you use a||b in a regex, you're saying "match 'a' or nothing or 'b'".my $h1=$1;my $m2=$2; my $s3=$3; if ($h1 =~ /13||14||15||16||17||18||19||20||21||22||23||24/) {
There's also no point in saying "if $str matches /foo/, substitute 'bar' for /foo/". Just say "substitute 'bar' for /foo/", and if any matches are found, they'll be changed. The idiom s/a/b/ if /a/ is pointless.
And there's no reason to use regexes here. $h1 is just a number now. This logic should be written as$h1=~s/13/1/; $h1=~s/14/2/; $h1=~s/15/3/; $h1=~s/16/4/; $h1=~s/17/5/; $h1=~s/18/6/; $h1=~s/19/7/; $h1=~s/20/8/; $h1=~s/21/9/; $h1=~s/22/10/; $h1=~s/23/11/; $h1=~s/24/12/;
my $pm = 0; # assume AM if ($h1 > 12) { $pm = 1; $h1 -= 12; }
|
|---|