ppp has asked for the wisdom of the Perl Monks concerning the following question:

I am a perl beginner and trying since sveral hoors and my code is :

use Win32::GuiTest (':ALL'); use Net::Address::IP::Local; use IO::Handle; use Browser::Open ('open_browser'); use strict; use Time::Local; use File::Copy; use warnings; no warnings 'uninitialized'; my $counter = 0; my($address) = @_; my $str; my $flag=0; my $file = 'C:\ListOfIpAdress.txt'; open my $fhi, '<', 'C:\ListOfIpAdress.txt', or die "Could not open +file $!"; open my $fho, '>', 'C:\ListOfIpAdress2.txt', or die "Could not ope +n file $!"; while (my $line = <$fhi>) { my ($a1, $a2, $a3, $Day, $sMonth, $sDate, $sTime, $SYear,$a9, + $a10) = 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"; ##WHY IT DONT PRINT check1 and check2, A +ctually i have to delete ListOfIpAdress1.txt and rename ListOfIpAdres +s2.txt by ListOfIpAdress.txt close $fhi; 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"; } }

And file ListOfIpAdress.txt contains this data (YOU CAN TEST MY CODE BY RUNNING IT AND SAVING BOTH FILES ON 1 LOCATION)

Address: 100.64.52.98 Time Fri Jan 16 21:44:35 2015 End Address: 100.65.0.172 Time Fri Jan 16 21:46:48 2015 End 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

Actually i want to delete ListOfIpAdress.txt and want to rename ListOfIpAdress2.txt by it's name so my code unlink ($file); do not work (for the same reason as it do not print check1 and check2). What is the reason that it do not work ? How to make it work? (INAFCT I TRIED THE "unlink" function from another file.pl which only contained the code to delete ListOfIpAdress.txt and it worked perfectly but dont work here so that i will get the cause of problem and will never repeat in future. ) Thankyou so much!!

Replies are listed 'Best First'.
Re: Why i am not able to print any statment after while loop
by Laurent_R (Canon) on Jan 18, 2015 at 09:52 UTC
    Unless I missed something, it appears that the $check1 and $check2 variables are not printed because they are not initialized anywhere in your code.

    I would recommend that you remove the following line from your code:

    no warnings 'uninitialized';
    as the 'uninitialized' warnings would tell you about potential errors.

    A couple of additional point.

    ... = split(/ {1,}/, $line);
    is better written:
    split(/ +/, $line);
    Take a look at localtime in list context, rather than using it in scalar context and then splitting the output.

    Update, 10:07 UTC: crossed out the first sentence: sorry, I misread too quickly your code, you are not using a $check1 but just printing the string "check1".

    Je suis Charlie.
Re: Why i am not able to print any statment after while loop
by Laurent_R (Canon) on Jan 18, 2015 at 10:22 UTC
    I slightly modified your code, as follows:
    use strict; use Time::Local; use warnings; my $counter = 0; my($address) = @_; my $str; my $flag=0; my $file = 'C:\ListOfIpAdress.txt'; open my $fho, '>', 'ListOfIpAdress2.txt' or die "Could not open file $ +!"; while (my $line = <DATA>) { 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"; } } __DATA__ Address: 100.64.52.98 Time Fri Jan 16 21:44:35 2015 End Address: 100.65.0.172 Time Fri Jan 16 21:46:48 2015 End 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
    and I do get the following output:
    $ perl logs.pl Total Difference in Hour is : 37.5744444444444 Total Difference in Hour is : 37.5375 Total Difference in Hour is : 36.3797222222222 Total Difference in Hour is : 36.1497222222222 Total Difference in Hour is : 35.4358333333333 Total Difference in Hour is : 35.3213888888889 n check1 n check2 File gone.
    And the ListOfIpAdress2.txt is duly created:
    $ cat ListOfIpAdress2.txt Address: 100.64.52.98 Time Fri Jan 16 21:44:35 2015 End Address: 100.65.0.172 Time Fri Jan 16 21:46:48 2015 End 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
    I do not know if this the result you are looking for, but I don't get the error you are reporting.

    Je suis Charlie.

      Hii thanks for the reply.

      Actually what i am trying to do is i have a file ListOfIpAdress.txt (please see below:) which i open and read it ($fhi do it in my code) i extract time from the string available.

      ListOfIpAdress2.txt Address: 100.64.52.98 Time Fri Jan 16 21:44:35 2015 End Address: 100.65.0.172 Time Fri Jan 16 21:46:48 2015 End 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

      Using my code i find the difference between the local time (current time and the time extracted from string).And in which ever line i get the time >36. I just ignore that line and read other lines and print them in ListOfIpAdress2.txt and then delete ListOfIpAdress.txt and Rename ListOfIpAdress2.txt to ListOfIpAdress.txt

      This is what i wanted to achieve and still not able to achieve. Could you please help me ? My previous code is able to print extracted lines in ListOfIpAdress2.txt but it is not able to delete ListOfIpAdress.txt and rename it to ListOfIpAdress.txt.

      Please alo let me know why it do not print"check1 and check2" again if in your c0ode i read the file like this " open my $fhi, '<', 'C:\shekhar_Axestrack_Intern\WindowCreation\ListOfIpAdress.txt', or die "Could not open file $!";"

        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.

        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.
Re: Why i am not able to print any statment after while loop
by LanX (Saint) on Jan 18, 2015 at 10:02 UTC