Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
Hi,<br><br>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! <br><br>If I’m right the script underneath should read in the all variables from the datafile:<br><br>$variables_file = “../data_file”;<br><br>sub variables_read {<br> # read in the data file<br> open (VARIABLES_FILE, &quot;&lt; $variables_file&quot;) or &error(&quot;Could not open data file at line &quot;, __LINE__);<br> @variables_data = &lt;VARIABLES_FILE&gt;;<br> chomp @variables_data;<br> close VARIABLES_FILE;<br>}<br><br>The next steps are:<br>1. identify the corresponding variable and add 1 to it’s value.<br>2. save it into the datafile.<br><br>This is a bit to much for me right now. Is there somebody who can help me with it.<br><br>raoul<br>
 
What you'd like to do is a bit fiddly with text files...<br><br>The line:<br><br>@variables_data = &lt;VARIABLES_FILE&gt;;<br><br>will actually read every line in the file and stash them into your array (@variables_data)<br><br>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<br><br>how many lines in the file?<br><br>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 <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
How's this:<br><br>#!/usr/bin/perl<br><br># cgi library<br>use CGI_Lite;<br>my $cgi=new CGI_Lite;<br>my %in=$cgi-&gt;parse_form_data;<br><br># get input variable<br>my $var = $in{'VAR'};<br><br>my $file = “../data_file”;<br><br># open and read file into array @data<br>open (FILE, &quot;$file&quot;) ¦¦ die;<br>my @data = &lt;FILE&gt;;<br><br># loop through array looking for your var<br>foreach my $x (@data)<br>{<br>&nbsp;&nbsp;#if it matches your var, increment the counter<br>&nbsp;&nbsp;if ($data[$x] =~ /^($var)=(\d*)$/)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;# increment it!<br>&nbsp;&nbsp;&nbsp;&nbsp;my $num = $2;&nbsp;&nbsp;$num++;<br>&nbsp;&nbsp;&nbsp;&nbsp;$data[$x] = &quot;$var=$num&quot;;<br>&nbsp;&nbsp;}<br>}<br><br># finally print the array back to the file and close it<br>print FILE @data;<br>close (FILE);<br><br>This assumes that your data file looks something like this:<br>ducks=4<br>devils=10<br>rangers=8<br><br>where there is a variable, then an equal sign, then a number.&nbsp;&nbsp;And each variable-number pair are on a single line (ie newline \n separated).<br><br>Sincerely,<br><br>Tom Anderson<br>CEO, Order amid Chaos, Inc.<br><A HREF=" TARGET="_new">
 
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.<br><br>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!<br><br>Raoul <br><br>
 
Hi again,<br><br>Tom, I tried your script but it causes an internal error.<br><br>I changed some things <br><br>like: 'my $in' instead of 'my %in'<br><br>and<br><br>“../data_file.txt”; instead of “../data_file”;<br><br><br>without success!<br><br>chmodded it 755 and the folder with datafile and datafile itself 777.<br><br>Any idea what is going wrong?<br><br>Raoul
 
Well, first you should look at your error_log and see what it's telling you is wrong.&nbsp;&nbsp;I wrote this off of the top of my head, so there may be some syntax errors.&nbsp;&nbsp;At a glance it looks right though.&nbsp;&nbsp;Do you have CGI_Lite installed?<br><br>perl -MCPAN -eshell<br>&gt;install CGI_Lite<br><br>Does the text file exist and does it have permissions set correctly?<br><br>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. <br><br>Sincerely,<br><br>Tom Anderson<br>CEO, Order amid Chaos, Inc.<br><A HREF=" TARGET="_new">
 
Didn't get it work yet, just one question. The modifications I made, are those correct. Or is the original script correct?
 
rtb1,<br><br>If you subsistute this line from Tom's (rather good) script<br><br>my $var = $in{'VAR'};<br><br>with<br><br>my $var = 'ducks';<br><br>the script will run without any dependance upon CGI-Lite as long you have your data-file set up correctly. (as tom describes)<br><br>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)<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Looking back over it again, I'm not sure if that foreach statement is right.&nbsp;&nbsp;Try replacing<br><br>foreach my $x (@data)<br><br>with this<br><br>for (my $x=0; $x&lt;@data; $x++)<br><br>Or you could always use DBI with an RDMS ;)<br><br>Sincerely, <br><br>Tom Anderson<br>CEO, Order amid Chaos, Inc.<br><A HREF=" TARGET="_new">
 
Thanks again for all your help, I'm going to try both your modifications.<br><br>btw. probably it will run over a couple of hundred records,<br>but let's start to get this working first,<br><br>Raoul
 
I'm sure it is me as I still can't get it work, (sorry about this).<br><br>Just to minimise possible errors from my side:<br><br>Was I right to change:<br><br>my $file = “../data_file”;<br><br>into<br><br>my $file = “../data_file.txt”;<br><br>and can I check this script from the browser or should I always pass a variable when running this script?<br><br>Raoul<br><br>btw I chmodded both the datafolder and the datafile 777 and the path is set correctly so I guess this should be fine.
 
You can name the data file whatever you want.<br><br>You should always pass a variable, otherwise it won't know what to increment.&nbsp;&nbsp;You can do this on the command line or in the query string like this thescript.pl?VAR=ducks.&nbsp;&nbsp;BTW, the script won't work as is (it won't increment anything) unless the datafile exists and it has data in it.&nbsp;&nbsp;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.<br><br>Sincerely, <br><br>Tom Anderson<br>CEO, Order amid Chaos, Inc.<br><A HREF=" TARGET="_new">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top