regarding a counter file

A

andy

My requirement is as below:
I want a counter of 3 numbers and it will count and keep the last count
as it is.
The second time i open and run this script and press count buttons it
should start counting from the last counter value.
Each time a count button is pressed it should be loged in to a txt file
with date and time.
I have written the script but not able to print the date and time in to
the log file.
The most important thing is that i want to save the last count and the
counter should start from the last saved counter when i run this script
again.
Please go through the script and suggest something.
Thanks in advance.



The below is my script:
#!\c\perl\bin
#######################################################################################
use strict;
#use warnings;
use Tk;
use Win32::Console;
Win32::Console::Free();

my $mw = new MainWindow; # Main Window
my $frm_name = $mw -> Frame() -> pack();

my $But_5 = $mw -> Button(-text=>"Counter of 5", -command
=>\&Addition_5) -> pack();
my $But_10 = $mw -> Button(-text=>"Counter of 10", -command
=>\&Addition_10) -> pack();
my $But_20 = $mw -> Button(-text=>"Counter of 20", -command
=>\&Addition_20) -> pack();

my $Save = $mw -> Button(-text=>" Save ", -command =>\&Save_Resulet)
-> pack();

my $Res;
sub Save_Result
{
$Res=$Res;
#&Saved_Counter;
}

#Text Area
my $txt = $mw -> Text(-width=>45, -height=>8,-background =>
'white',-font => "Arial 11 bold", -foreground => "black") -> pack();
open (fh,">>counter_log.txt") or die "can't open counter_log.txt $!\n";
print fh "###### Started ######\n\n";
MainLoop;
#This is executed when the button is pressed

sub Addition_5
{
print fh "check\n";
print fh `date /t`;
print fh `time /t`;
$Res=$Res+5;
$txt -> insert('end',"The Addition is: $Res\n\n");
print fh "The Addition is: $Res\n";
}
sub Addition_10
{
$Res=$Res+10;
$txt -> insert('end',"The Addition is: $Res\n\n");
print fh `date /t`;
print fh `time /t`;
print fh "The Addition is: $Res\n";
}
sub Addition_20
{
$Res=$Res+20;
$txt -> insert('end',"The Addition is: $Res\n\n");
print fh `date /t`;
print fh `time /t`;
print fh "The Addition is: $Res\n";
}
close(fh);
 
A

andy

andy said:
My requirement is as below:
I want a counter of 3 numbers and it will count and keep the last count
as it is.
The second time i open and run this script and press count buttons it
should start counting from the last counter value.
Each time a count button is pressed it should be loged in to a txt file
with date and time.
I have written the script but not able to print the date and time in to
the log file.
The most important thing is that i want to save the last count and the
counter should start from the last saved counter when i run this script
again.
Please go through the script and suggest something.
Thanks in advance.



The below is my script:
#!\c\perl\bin
#######################################################################################
use strict;
#use warnings;
use Tk;
use Win32::Console;
Win32::Console::Free();

my $mw = new MainWindow; # Main Window
my $frm_name = $mw -> Frame() -> pack();

my $But_5 = $mw -> Button(-text=>"Counter of 5", -command
=>\&Addition_5) -> pack();
my $But_10 = $mw -> Button(-text=>"Counter of 10", -command
=>\&Addition_10) -> pack();
my $But_20 = $mw -> Button(-text=>"Counter of 20", -command
=>\&Addition_20) -> pack();

my $Save = $mw -> Button(-text=>" Save ", -command =>\&Save_Resulet)
-> pack();

my $Res;
sub Save_Result
{
$Res=$Res;
#&Saved_Counter;
}

#Text Area
my $txt = $mw -> Text(-width=>45, -height=>8,-background =>
'white',-font => "Arial 11 bold", -foreground => "black") -> pack();
open (fh,">>counter_log.txt") or die "can't open counter_log.txt $!\n";
print fh "###### Started ######\n\n";
MainLoop;
#This is executed when the button is pressed

sub Addition_5
{
print fh "check\n";
print fh `date /t`;
print fh `time /t`;
$Res=$Res+5;
$txt -> insert('end',"The Addition is: $Res\n\n");
print fh "The Addition is: $Res\n";
}
sub Addition_10
{
$Res=$Res+10;
$txt -> insert('end',"The Addition is: $Res\n\n");
print fh `date /t`;
print fh `time /t`;
print fh "The Addition is: $Res\n";
}
sub Addition_20
{
$Res=$Res+20;
$txt -> insert('end',"The Addition is: $Res\n\n");
print fh `date /t`;
print fh `time /t`;
print fh "The Addition is: $Res\n";
}
close(fh);


one more thing each time i press a count button i want to clear the
previous text from the
text box of the GUI.
How will i do that.
 
J

John W. Krahn

andy said:
My requirement is as below:
I want a counter of 3 numbers and it will count and keep the last count
as it is.
The second time i open and run this script and press count buttons it
should start counting from the last counter value.

use Tie::File;

my $counter_file = "$ENV{HOME}/.counter_file";

tie my @counters, 'Tie::File', $counter_file
or die "Cannot open '$counter_file' $!";

# initialize @counters if this is the first time
@counters = ( 1, 2, 3 ) unless @counters;

my $current_counter = shift @counters;

# code that uses the $current_counter


push @counters, $current_counter;

untie @counters;


Each time a count button is pressed it should be loged in to a txt file
with date and time.
I have written the script but not able to print the date and time in to
the log file.

print LOG_FILE scalar localtime, " counter = $current_counter\n";

Or if you want more control over the date and time formats:

use POSIX 'strftime';

print LOG_FILE strftime( '%x %X', localtime ), " counter = $current_counter\n";




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top