user3579888
user3579888

Reputation: 11

"less than or equal to" operator not working in if condition

my $path="/mnt/splash/logs/";
my $serv= sprintf("splash-%s-%02d%02d%02d-%s",$server,$year,$mon,$date,$hourStart);
my $eserv= sprintf("splash-%s-%02d%02d%02d-%s",$server,$eyear,$emon,$edate,$hourEnd);
print $serv;
print $eserv;
#OPEN THE SPLASH LOGS DIRECTORY AND READ THE FILES PRESENT THEN STORE IT IN ARRAY.

my $remote_dir=sprintf("ssh %s 'ls %s'",$server,$path);
chomp $remote_dir;
my @list=`$remote_dir`;

#FINDING THE ONLY SPLASH LOGS FILES FROM THE DIRECTORY AND STORED IN AN ARRAY USING PUSH METHOD.
my @srt_list= sort @list;
my @arlist=();
my @arrlog=();
my $i=0;
my $filt;
for(@srt_list) {
    if ($_ ge $serv && $_ le $eserv) {
        #my $filter= sprintf("$_\n");
        chomp $_;

        push (@arrlog, $_);
    }
}

Above is the perl code i used to retrieve the logs from the array. I used if condition with the ge and le operators but if i searched for the log file between 20160628-0400.log to 20160629-1000.log this code is giving the logs from 20160628-0400.log to 20160629-0900.log this is not retrieving the log 20160629-1000.log which the end time log i m searching. i dunno how this happening i used le operator that means it should select the logs that is less than or equal log right?

Please clarify this. i really don't wanna add any other check to retrieve the end time log.

Upvotes: 1

Views: 227

Answers (1)

Sobrique
Sobrique

Reputation: 53478

Without seeing your input data, I'm going to have to guess.

But from what you've got, I'm guessing you're comparing:

20160629-1000 with 20160629-1000.log

The suffix on the end makes it alphabetically 'greater'.

print "yes" if "20160629-1000" le "20160629-1000.log";
print "no" if  "20160629-1000.log" le "20160629-1000";

That's because le is a stringwise comparison, not a numeric one.

The simple fix would be to strip off the suffix:

for (@srt_list) {
    my ($compare) = m/(splash.*[\d\-]+)\.log/;
    print "Comparing $compare with $serv and $eserv\n";    
    if ($compare ge $serv && $compare le $eserv) {
        chomp $_;

        push (@arrlog, $_);
    }
}

Upvotes: 3

Related Questions