Gossamer Forum
Home : Products : DBMan : Customization :

Question on Arrays

Quote Reply
Question on Arrays
I'm a little confused on the proper way to handle information stored in an array. I'm trying to access a 4 field array, increment one field and perform an addition on another. Then save the array again with the new info. I can pull the array fine. I can get the math to show the correct info, but when I push the array back up, it doesn't store the new values.
Here is what I'm trying to do: I want to get a record of everytime certain users submit with a simple counter. Then I want to store a second field (numerical) and at the same time,add it to the last value, eventually giving a total # for that field.

Here is the code I've got so far, but I think I'm calling the fields wrong:
open (DB, ">>/users/support/www/docs/cgi-bin/dbman2/ajcen.db");
@temp = <DB>;

@values = split (/\|/);

$temp[0] = ++$temp[0];

$temp[1] = $temp[1] + 5;

print DB &join_encode(%in);

close DB;

This will always be a static db file and will only be accessed when that user submits, so I'm not worried about file locking. The 5 will eventually be replaced by a variable, but I made it static while I was testing. Am I on the right track here, or completely wrong? As far as I'm concerned, the simpler this script is the better, because it doesn't have to do much and there are no scenarios that could make it fail. The code would be appreciated, but if it would be nice you could explain it, I'd like to figure this stuff out.

Thanks,

KipT - Perl novice thrown to the wolves
Quote Reply
Re: Question on Arrays In reply to
Ok. I've found another code that might be easier. While this one doesn't store the information, I think it's got me closer:
open (DB, ">>/users/support/www/docs/cgi-bin/dbman2/ajcen.db");
@temp = <DB>;
($total, $minutes) = @temp;
$total = ++$total;
$minutes = $minutes + 5; @temp = ($total, $minutes);
print DB;
close DB;

Does this look better and what would make it store the DB correctly?
Quote Reply
Re: Question on Arrays In reply to
Okay. I'll try to explain it, although I'm not sure I'm good enough at Perl to give a workable explanation.

In your line

open (DB, ">>/users/support/www/docs/cgi-bin/dbman2/ajcen.db");

the >> means to open the file to append data at the end of the file. It won't read the file contents.

Replace the >> with < to be able to read the contents of the file.

----------------------------
@temp = <DB>;

is correct. At this point, your entire database is in an array called @temp. Individual records would be accessed by using $temp[0] (for the first record, since array numbering starts at 0), $temp[1], $temp[2], etc.
---------------------------

In order to break up each record into field values, you have to take each $temp[n] variable at a time. The easiest way is to use a loop.

Code:
foreach $line (@temp) {
do something
}

The script will go through each of the $temp[n] values and assign the value to a variable called $line. (You can use any variable name you want.)
------------------------------

To split up the $line into values, use

Code:
@values = split '|',$line;

This would be within the "foreach" loop I gave you above. The split command will take the field values and put them into an array that you can access like you did the @temp array -- $values[0], $values[1], etc. This time, though, each $values[n] will be a field value.

----------------------
$temp[0] = ++$temp[0];

$temp[1] = $temp[1] + 5;

You don't want to try accessing the $temp array, since that is the entire line. Use the appropriate $values[n] variable instead.

---------------------
print DB &join_encode(%in);

The %in hash is a completely different thing from what you've been working with already. You don't want to use it. You also don't want to use &join_encode, because that requires a hash and not an array. (A hash is an associative array that uses labels instead of numbers to access the individual elements. When you use $rec{'FieldName'} in html_record, for example, you're using a hash.

Instead, I would use

$output = join '|', @values;
print DB output;
---------------------
I hope I didn't give you more code than you wanted, but I didn't know how to explain it any other way. Smile



------------------
JPD


[This message has been edited by JPDeni (edited August 04, 1999).]