×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

reading from file - change value of variable - save it , help needed!

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

RE: reading from file - change value of variable - save it , help needed!

What you'd like to do is a bit fiddly with text files...

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!

How's this:

#!/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!

(OP)
Yes Mike you're right but I have to start somewhere and from other scripts I new this would at least read the data file. That's why I am (was) stucked with the next part.

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!

(OP)
Hi again,

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!

Well, first you should look at your error_log and see what it's telling you is wrong.  I wrote this off of the top of my head, so there may be some syntax errors.  At a glance it looks right though.  Do you have CGI_Lite installed?

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!

(OP)
Didn't get it work yet, just one question. The modifications I made, are those correct. Or is the original script correct?

RE: reading from file - change value of variable - save it , help needed!

%in is an associative array (or hash).  It must use the %

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!

rtb1,

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!

Looking back over it again, I'm not sure if that foreach statement is right.  Try replacing

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!

(OP)
Thanks again for all your help, I'm going to try both your modifications.

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!

(OP)
I'm sure it is me as I still can't get it work, (sorry about this).

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 can name the data file whatever you want.

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close