ExifTool Forum

General => Other Discussion => Topic started by: Alan Clifford on August 18, 2015, 02:05:48 PM

Title: Perl query
Post by: Alan Clifford on August 18, 2015, 02:05:48 PM
I have a test that seems to be working differently in two different constructs.

$hour =~ /(.*):/

From the code fragment below, this is the difference:

hour: +0:30   hr:  +0
hour: -0:30   hr:  -0
hour: 0:30   hr:  0:30
hour: +0:30   hr:  +0
hour: -0:30   hr:  -0
hour: 0:30   hr:  0


#!/usr/bin/perl
#
use strict;
use warnings;

my $hour;
my @allhours ;
my $hr ;
push @allhours, "+0:30" ;
push @allhours, "-0:30" ;
push @allhours, "0:30" ;

foreach $hour ( @allhours ) {
  $hour =~ /(.*):/ and $hr = $1 or $hr = $hour ;
  print "hour: $hour\thr:  $hr\n" ;
}

foreach $hour ( @allhours ) {
  if ($hour =~ /(.*):/ ) {
    $hr = $1 ;
  }
  else {
    $hr = $hour ;
  }
  print "hour: $hour\thr:  $hr\n" ;
}

Title: Re: Perl query
Post by: Phil Harvey on August 18, 2015, 02:23:51 PM
Hi Alan,

Tricky.

Your problem is that "0" is false in Perl (so $hr = $1 is false if $hr is "0").  Try this maybe:

  $hr = $hour =~ /(.*):/ ? $1 : $hour;

TIMTOWTDI :)

- Phil
Title: Re: Perl query
Post by: Alan Clifford on August 18, 2015, 05:53:48 PM
As a Perl novice, I think I was trying to be too clever.