OK, I tried again with an external input file so that it looks more like your code. And it succeeds perfectly. The only thing that does not get done is the renaming of ListOfIpAdress2.txt into ListOfIpAdress.txt, because you don't have any code doing that.

Below are the code I tested and the results.

The code I used:

use strict; use Time::Local; use warnings; my $counter = 0; my($address) = @_; my $str; my $flag=0; my $file = 'ListOfIpAdress.txt'; open my $fho, '>', 'ListOfIpAdress2.txt' or die "Could not open file $ +!"; open my $fhi, '<', 'ListOfIpAdress.txt' or die "Could not open file $! +"; while (my $line = <$fhi>) { my ($a1, $a2, $a3, $Day, $sMonth, $sDate, $sTime, $SYear,$a9, $a1 +0) = split(/ {1,}/, $line); my $str1= $sMonth." ".$sDate." ".$sTime ; my $scTime = localtime; my ( $cDay, $cMonth, $cDate, $cTime, $cYear,$Syear) = split(/ {1, +}/, $scTime); my $str2= $cMonth." ".$cDate." ".$cTime ; use constant Year => 2015; my $t1 = convert($str1); my $t2 = convert($str2); my $Diff= $t2 - $t1; my $div = $Diff/3600; print "Total Difference in Hour is : $div \n\n"; print $fho $line unless $div > 38; } print "\nn check1 \n\n"; close $fho; print "\nn check2 \n\n"; unlink ($file); if(-e $file) { print "File still exists!"; } else { print "File gone."; } sub convert { my $dstring = shift; my %m = ( 'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11 ); if ($dstring =~ /(\S+)\s+(\d+)\s+(\d{2}):(\d{2}):(\d{2})/) { my ($month, $day, $h, $m, $s) = ($1, $2, $3, $4, $5); my $mnumber = $m{$month}; # production code should handle erro +rs here timelocal( $s, $m, $h, $day, $mnumber, 1900 ); } else { die "Format not recognized: ", $dstring, "\n"; } }
The screen display:
$ perl logs.pl Total Difference in Hour is : 38.7786111111111 Total Difference in Hour is : 38.7416666666667 Total Difference in Hour is : 37.5838888888889 Total Difference in Hour is : 37.3538888888889 Total Difference in Hour is : 36.64 Total Difference in Hour is : 36.5255555555556 n check1 n check2 File gone.
The directory listing showing that the source file has been duly deleted:
$ ls -lrt (...) -rwxr-xr-x 1 Laurent None 2024 18 janv. 12:31 logs.pl -rw-r--r-- 1 Laurent None 226 18 janv. 12:31 ListOfIpAdress2. +txt
And the content of ListOfIpAdress2.txt:
$ cat ListOfIpAdress2.txt Address: 100.64.58.219 Time Fri Jan 16 22:56:16 2015 End Address: 100.65.5.25 Time Fri Jan 16 23:10:04 2015 End Address: 100.64.8.184 Time Fri Jan 16 23:52:54 2015 End Address: 100.65.13.135 Time Fri Jan 16 23:59:46 2015 End
So all that is still needed is the final renaming of ListOfIpAdress2.txt to ListOfIpAdress.txt.

Please show your output, tell us if the source file gets deleted, show us any error message or warning. There are quite a few things that could be improved in your code, but it should basically work, since the slightly modified version I used is working on my computer (except of course for the final renaming that needs to be added to your code).

Je suis Charlie.

In reply to Re^3: Why i am not able to print any statment after while loop by Laurent_R
in thread Why i am not able to print any statment after while loop by ppp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.