in reply to Compare script

I don't have much time at the moment to review the code specifically, but hopefully what I've done may help a bit.

I think that you should be passing your args into the sub, instead of using global vars. This causes much grief, beyond the problems it would cause if you wanted to move the sub to a library file. Also, prototypes within Perl functions don't do what most think they do. I can't think of a single link that describes this, but the archives, and Google explains it well

I quickly put the following working code snip together to hopefully give you some ideas. It was hard to tell what you meant by "it wouldn't compare them" without knowing what you expected as a result:

#!/usr/bin/perl use strict; use warnings; my $mtime = ( stat ( "/home/steve/devel/ISP/lib/ISP/User.pm" ))[9]; my $other_mtime = 999999999; my $cmp_result = compare ( $mtime, $other_mtime ); print "$cmp_result\n"; sub compare { my ( $mtime, $other_mtime, ) = @_; my $cmp_result; if ( $mtime > $other_mtime ) { $cmp_result = "mtime_is_higher"; } else { $cmp_result = "mtime_is_lower"; } return $cmp_result; }

Hope it helps a bit

Steve

Replies are listed 'Best First'.
Re^2: Compare script
by TheRedcoat (Novice) on Aug 31, 2009 at 16:38 UTC
    Thank you to all who helped. Your guess was indeed correct I was looking at the dir attempting to see what file was newest and then output that file name. I have the perl working, the code may not be the nicest but it works. I realised I was not keeping the old compare time when looking at graff's example. I unfortunately cannot use a lot of modules. We are running an older version of perl at work and they refuse to update. I can't even use NET::Telnet Here it is as it works followed by the output:
    #use strict; my $comparetime="0"; sub compare; ##defines locations to look for files $Clinrxdir="D:\\StoreAccess\\shared\\sup shared\\"; ##reads DIR opendir(DIR,$Clinrxdir); @Files= readdir(DIR); closedir(DIR); #for every file retured do the following foreach $cpupdate (@Files){ ##if the file starts with CP_ checks it's timestamp if($cpupdate =~ m/\s*cp_/i){ ##I did not write this section I was using code I read up on using goo +gle and found in other various ##scripts we used, for me the fact it broke it down in to a simple int + style var made life easier. my $m_time = (stat($Clinrxdir.$cpupdate))[9]; my ($i_wday, $i_month, $day, $year) = (localtime($m_time))[6,4,3,5]; ##prints what files where found and time stamps print "Timestamp for " . $cpupdate . " " . $m_time ."\n"; ##Attempting to use this sup to compare time stamps and change the $fi +le var to the one with most recent ##stamp compare($m_time); } } ##$file is pulled from the compare sub print "The oldest file= " . $file . "\n"; print "The time stamp for this file is " . $oldcomp . "\n"; system("pause"); ##this was meant to work in the following manner: look at time stamp. +if it's older than the one ##currently stored do nothing, if it's newer change $file to be the fi +le name of the newer file. ##this is where I realised my folly thanks to graff's code.. I was not + keeping anything to compare to ##other than the new string sub compare($){ local( $string ) = shift; if($comparetime > $oldcomp){ $file=$cpupdate; $oldcomp=$string;} else{$comparetime = $string;} }

    Timestamp for CP_APR09.exe 1241465790
    Timestamp for CP_FEB09.exe 1236031283
    Timestamp for CP_JUL09.exe 1249498031
    Timestamp for CP_JUN09.EXE 1247593421
    Timestamp for CP_MAR09.EXE 1238771856
    Timestamp for CP_MAY09.EXE 1244056821
    The oldest file= CP_JUL09.exe
    The time stamp for this file is 1249498031

    That is as the output looks when returned in perl once again thank you for your help