Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrong time returned by stat function 1

Status
Not open for further replies.

carg1

MIS
Aug 19, 2003
20
US
I don't understand this problem. This script is meant to read a text file from a content filter and let you query it by any text input. For some reason, it won't return the right date modified date in the section where I ask it to call for it. I want to use that as part of the filename when it saves the output.

Code:
use warnings;
my $TheDB ;
my $Theline ;
my $SuccessCount = 0 ;

print "Type input filename:  ";
$TheDB = <STDIN>;
  chomp($TheDB);

# Open the database file but quit if it doesn't exist
open(INDB, $TheDB) or die &quot;The database $TheDB could &quot; .
  &quot;not be found.\n&quot;;

#Creates a list called Ops and stores the stat function ran on the previous input.  $Stats runs the date modified option on Ops
$Ops = stat($TheDB);
@Stats = $Ops[9];

#Converts the date to a readable format
($min,$hour,$mday,$mon,$year) = localtime(@Stats);
$RMonth = $mon + 1; #Adds one to month to obtain the correct month
$SYear = ($year % 100); #Runs modulo arithmetic on the year to get the correct short year
@DateMod = ($RMonth, $mday, $SYear, $hour, $min);

#Stores the value of Ops, converted into a readable format, into the variable $Stuff
$Filename = sprintf(&quot;%02d%02d%02d%02d%02d&quot;, @DateMod);

#Creates a new file named after the contents of $Filename with &quot;.txt&quot; appended 
open(OUTDB, &quot;>d:\\&quot;.&quot;hits&quot;.&quot;$Filename&quot;.&quot;.txt&quot;) or die &quot;File $Filename did not save successfully.&quot;;

print &quot;\nType in search string:  &quot;;
$dosearch = <> ;
 chomp($dosearch) ;

print &quot;\n &quot; ;

while(<INDB>) { # Loop forever
  $Theline = $_;
  chomp($Theline) ;

   if($Theline =~m/$dosearch/i) {
      $SuccessCount = $SuccessCount + 1 ;
       print (&quot;$Theline \n &quot;);
       print OUTDB (&quot;$Theline \n &quot; );     
    } # End of if

  } # End of while(<INDB>)

print &quot;\nProgram finished.\n $SuccessCount Records Found\n\n&quot;;

print &quot;Output saved.\n&quot;;

print &quot;$Filename&quot;.&quot; was the filename used&quot;

It creates the file with no problem, but with it returning the wrong time, it's not helping. When I separated the code that creates the filename, it worked fine. Here's that code:

Code:
#Asks you which file you want to see stats on and seeks your input
print &quot;Which file would you like to see statistics on? [dir:\\path\\filename] &quot;;
$MyFile = <STDIN>;
chomp($MyFile);

#Creates a list called Ops and stores the stat function ran on the previous
#input.  $Stats runs the date modified option on Ops.
@Ops = stat($MyFile);
$Stats = @Ops[9];

#Converts the date to a readable format
($Second, $Minute, $Hour, $DayofMonth, $Month, $Year) = localtime($Stats);
$RMonth = $Month + 1; #Adds one to month to obtain the correct month
$SYear = ($Year % 100); #Runs modulo arithmetic on the year to get the correct short year

#Stores the value of Ops, converted into a readable format, into the variable $Stuff
$Stuff = sprintf(&quot;%02d%02d%02d%02d%02d&quot;, $RMonth, $DayofMonth, $SYear, $Hour, $Minute);

#Creates a new file named after the contents of $Stuff with &quot;.txt&quot; appended 
open(TESTING, &quot;>d:\\&quot;.&quot;hits&quot;.&quot;$Stuff&quot;.&quot;.txt&quot;) or die &quot;Didn't work buddy.  Sorry.&quot;;

print TESTING &quot;Words&quot;x25;
print &quot;Don't worry, it's saved.\n&quot;;
print &quot;$Stuff was the filename used.&quot;;


What am I doing wrong?
 
You have a number of problems with the above script, including how you declared the Stat function and how you split the localtime value. Try the following:

use warnings;
my $TheDB ;
my $Theline ;
my $SuccessCount = 0 ;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);

print &quot;Type input filename: &quot;;
$TheDB = <STDIN>;
chomp($TheDB);

# Open the database file but quit if it doesn't exist
open(INDB, $TheDB) or die &quot;The database $TheDB could &quot; . &quot;not be found.\n&quot;;

#Creates a list called Ops and stores the stat function ran on the previous input. $Stats runs the date modified option on Ops
$Ops = (stat($TheDB))[9];
#Converts the date to a readable format
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($Ops);
$RMonth = $mon + 1; #Adds one to month to obtain the correct month
$SYear = ($year % 100); #Runs modulo arithmetic on the year to get the correct short year
@DateMod = ($RMonth, $mday, $SYear, $hour, $min);
#Stores the value of Ops, converted into a readable format, into the variable $Stuff
$Filename = sprintf(&quot;%02d%02d%02d%02d%02d&quot;, @DateMod);
print &quot;filename is $Filename\n&quot;;

#Creates a new file named after the contents of $Filename with &quot;.txt&quot; appended
open(OUTDB, &quot;>d:\\&quot;.&quot;hits&quot;.&quot;$Filename&quot;.&quot;.txt&quot;) or die &quot;File $Filename did not save successfully.&quot;;

print &quot;\nType in search string: &quot;;
$dosearch = <> ;
chomp($dosearch) ;

print &quot;\n &quot; ;

while(<INDB>) { # Loop forever
$Theline = $_;
chomp($Theline) ;

if($Theline =~m/$dosearch/i) {
$SuccessCount = $SuccessCount + 1 ;
print (&quot;$Theline \n &quot;);
print OUTDB (&quot;$Theline \n &quot; );
} # End of if

} # End of while(<INDB>)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top