reading from file - change value of variable - save it , help needed!
reading from file - change value of variable - save it , help needed!
(OP)
Hi,
All of this is rather new to me although I think I have overwon the first step and I’m starting to like this. What I’m trying to do is executing a cgi script which should read a datafile and only add 1 to the value of the corresponding variable in the datafile that was send with the form!
If I’m right the script underneath should read in the all variables from the datafile:
$variables_file = “../data_file”;
sub variables_read {
# read in the data file
open (VARIABLES_FILE, "< $variables_file") or &error("Could not open data file at line ", __LINE__);
@variables_data = <VARIABLES_FILE>;
chomp @variables_data;
close VARIABLES_FILE;
}
The next steps are:
1. identify the corresponding variable and add 1 to it’s value.
2. save it into the datafile.
This is a bit to much for me right now. Is there somebody who can help me with it.
raoul
All of this is rather new to me although I think I have overwon the first step and I’m starting to like this. What I’m trying to do is executing a cgi script which should read a datafile and only add 1 to the value of the corresponding variable in the datafile that was send with the form!
If I’m right the script underneath should read in the all variables from the datafile:
$variables_file = “../data_file”;
sub variables_read {
# read in the data file
open (VARIABLES_FILE, "< $variables_file") or &error("Could not open data file at line ", __LINE__);
@variables_data = <VARIABLES_FILE>;
chomp @variables_data;
close VARIABLES_FILE;
}
The next steps are:
1. identify the corresponding variable and add 1 to it’s value.
2. save it into the datafile.
This is a bit to much for me right now. Is there somebody who can help me with it.
raoul
RE: reading from file - change value of variable - save it , help needed!
The line:
@variables_data = <VARIABLES_FILE>;
will actually read every line in the file and stash them into your array (@variables_data)
you might have wanted to do this - but you're calling chomp on the next line which implies you meant to just get a single line from the file, rather than the whole lot
how many lines in the file?
is it only one variable per line? if it is then you might want to think about using a DBM file - with these it's quite easy to read and write single records at a time
Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site
RE: reading from file - change value of variable - save it , help needed!
#!/usr/bin/perl
# cgi library
use CGI_Lite;
my $cgi=new CGI_Lite;
my %in=$cgi->parse_form_data;
# get input variable
my $var = $in{'VAR'};
my $file = “../data_file”;
# open and read file into array @data
open (FILE, "$file") ¦¦ die;
my @data = <FILE>;
# loop through array looking for your var
foreach my $x (@data)
{
#if it matches your var, increment the counter
if ($data[$x] =~ /^($var)=(\d*)$/)
{
# increment it!
my $num = $2; $num++;
$data[$x] = "$var=$num";
}
}
# finally print the array back to the file and close it
print FILE @data;
close (FILE);
This assumes that your data file looks something like this:
ducks=4
devils=10
rangers=8
where there is a variable, then an equal sign, then a number. And each variable-number pair are on a single line (ie newline \n separated).
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: reading from file - change value of variable - save it , help needed!
Tom this is exactly what the data file should look like and I will try the script you made. This is extremely helpful and a good way to learn more! Thanks a lot!
Raoul
RE: reading from file - change value of variable - save it , help needed!
Tom, I tried your script but it causes an internal error.
I changed some things
like: 'my $in' instead of 'my %in'
and
“../data_file.txt”; instead of “../data_file”;
without success!
chmodded it 755 and the folder with datafile and datafile itself 777.
Any idea what is going wrong?
Raoul
RE: reading from file - change value of variable - save it , help needed!
perl -MCPAN -eshell
>install CGI_Lite
Does the text file exist and does it have permissions set correctly?
BTW, when you implement this, you should add file locking in if this has the possibility of being accessed by more than one person at the same time.
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: reading from file - change value of variable - save it , help needed!
RE: reading from file - change value of variable - save it , help needed!
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: reading from file - change value of variable - save it , help needed!
If you subsistute this line from Tom's (rather good) script
my $var = $in{'VAR'};
with
my $var = 'ducks';
the script will run without any dependance upon CGI-Lite as long you have your data-file set up correctly. (as tom describes)
This approach is fine for smallish datafiles, few hundred records or so. Any more than that and you want to start looking at DBM files (which are dead easy and look a lot like Tom's code - just will run faster and you don't need to read and write the whole file when you change something)
Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site
RE: reading from file - change value of variable - save it , help needed!
foreach my $x (@data)
with this
for (my $x=0; $x<@data; $x++)
Or you could always use DBI with an RDMS ;)
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: reading from file - change value of variable - save it , help needed!
btw. probably it will run over a couple of hundred records,
but let's start to get this working first,
Raoul
RE: reading from file - change value of variable - save it , help needed!
Just to minimise possible errors from my side:
Was I right to change:
my $file = “../data_file”;
into
my $file = “../data_file.txt”;
and can I check this script from the browser or should I always pass a variable when running this script?
Raoul
btw I chmodded both the datafolder and the datafile 777 and the path is set correctly so I guess this should be fine.
RE: reading from file - change value of variable - save it , help needed!
You should always pass a variable, otherwise it won't know what to increment. You can do this on the command line or in the query string like this thescript.pl?VAR=ducks. BTW, the script won't work as is (it won't increment anything) unless the datafile exists and it has data in it. You'd have to add a few lines to the script if you wanted to be able to make up new variables as you went along and have them added to the data file.
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com