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!

updating data of a record in a data file?

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
I have this perl score_board script that I use for a game. It shows the final result of a player. I’m trying to change the script in a way that the score board is immediately updated with the scores of the separate parts within the game.

To accomplish this I added $ref to each record which is a unique number. When the player finishes a part within the game all variables (incl. $ref) are sent to the script.

The idea is to match $ref with the specific field in the data file and update this record.

This is what I had in mind (replace the data of the specific record for the new data) but the script is aborted due to compilation errors:

$count=0;
foreach $data(@scores_data) {
$count++;
@datax=split(/\|/, $data);
if ($newref eq "@datax[0]" and $newref== "@datax[0]") {
@scores_data[$count] = "$newref\|$part_1\|$part_2\|$total";
}
}

What is going wrong here?

Raoul,
[sig][/sig]
 
Raoul,

The fragment you've supplied compiles correctly under Perl v5.005_03 built for aix. I think the syntax error, if you have one, might be in a different part of the script.

[sig]<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>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
Hi Mike,

I just double checked it. When I delete the part above from the script it works fine.

This is how the original script starts:

use CGI qw:)cgi);
use CGI::Carp 'fatalsToBrowser';
use strict;

Maybe it has to do with this?

or, maybe somebody has an alternative way to achieve what I intend to do?

Raoul [sig][/sig]
 
If the above script is correct than maybe this is what the error causes:

First the data is read:

sub scores_read {
# read in the data file
open (SCORES_FILE, &quot;< $data_file&quot;) or &error(&quot;Could not open data file at line &quot;, __LINE__);
@scores_data = <SCORES_FILE>;
chomp @scores_data;
close SCORES_FILE;
}

The original script continues with:

sub scores_process {
# add on that new score and new name
unshift @scores_data, $newref . '|' . $abs_scores . '|' .$my_name . '|' . $my_score;
# sort scores (default sort)
}

I changed the beginning of this second part with an if-else statement (if player is new, add the player – if player already exists, identify the player and change the record):

if ($part_2 ne &quot;_&quot;){
$count=0;
foreach $data(@scores_data) {
$count++;
@datax=split(/\|/, $data);
if ($newref eq &quot;@datax[0]&quot; and $newref== &quot;@datax[0]&quot;) {
@scores_data[$count] = &quot;$newref\|$strokes_abs\|$my_name\|$my_score\|$part_1\|$part_2&quot;;
}
}
}
else
{
unshift @scores_data, $newref . '|' . $abs_scores . '|' .$my_name . '|' . $my_score;
}

My last thought was that the line ‘chomp @scores_data;’ in sub scores_read is causing the error. I don’t know what this exactly does but this is the only line which is not used (necessary) in the part that is doing the match of $newref with each record in the datafile and maybe that is what is causing the error?

Am I right about this?

I tried to change it this way:

Move the top part of sub scores_process to sub scores_read:

sub scores_read {
# read in the data file
open (SCORES_FILE, &quot;< $data_file&quot;) or &error(&quot;Could not open data file at line &quot;, __LINE__);
@scores_data = <SCORES_FILE>;
if ($part_2 ne &quot;_&quot;){
$count=0;
foreach $data(@scores_data) {
$count++;
@datax=split(/\|/, $data);
if ($newref eq &quot;@datax[0]&quot; and $newref== &quot;@datax[0]&quot;) {
@scores_data[$count] = &quot;$newref\|$strokes_abs\|$my_name\|$my_score\|$part_1\|$part_2&quot;;
}
}
chomp @scores_data;
}
else
{
unshift @scores_data, $newref . '|' . $abs_scores . '|' .$my_name . '|' . $my_score;
chomp @scores_data;
}
close SCORES_FILE;
}


But this is causing errors as well!

If I’m right that initially ‘chomp @scores_data’ is causing the error how can I change the script in a way it will work?

Raoul
[sig][/sig]
 
@scores_data is an array, so it contains lots of lines -- you could say something like

chomp($scores_data[0]);

to remove a newline from the first element of the array.

Try chomp($data) after the foreach statement

What errors are you getting by the way? [sig][/sig]
 
Thanks Mike,

Unfortunately your last suggestion doesn't work and I don't have access to an error log so I can't tell you what the error is.

Do you mean that &quot;chomp($scores_data[0]);&quot; will remove the first line of @scores_data?

Raoul [sig][/sig]
 
The chomp function removes any new-line character from a string -- and a string in Perl starts with a $ rather than a @ -- something that starts with a @ is a collection of strings (a list of scalars really...)

The version of scores_read below will fill up the array @scores_data with lines that have the new-line character removed.

[tt]
sub scores_read {
[tab]open (SCORES_FILE, &quot;< $data_file&quot;) ||
[tab][tab]die &quot;Could not open data file at line &quot; . __LINE__ . &quot;\n$!\n&quot;;
[tab]while(<SCORES_FILE>){
[tab][tab]chomp;
[tab][tab]push @scores_data;
[tab]}
[tab]close SCORES_FILE;
}
[/tt]

[sig][/sig]
 
Thanks for the explanation Mike. The way I have seen 'chomp' being used I couldn’t imagine that it would actually remove a complete line.

However, I changed the script a bit and it is working now. It seems that the line ‘use strict;’ has quite some influence on the variables and the use of variables outside a subroutine.

Raoul
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top