Gossamer Forum
Home : General : Perl Programming :

Arrays: Adding a new field to the front

Quote Reply
Arrays: Adding a new field to the front
Here's my goal --- open an existing database, add a unique number in order to be able to differentiate each line between the next, read that number so when asked "Would you like to delete this record? (Y/N)", that line can be deleted through a common form INPUT Checkbox and the remaining lines in the database will be pushed up to the top of the database so everything's nice and pretty again. Sounds simple, right? Not for a Perl newbie it's not. Frown

This is an example of what my text database looks like:

Oct 10|Larry|10012|123 Any Street
Oct 15|Moe|85224|5 Main Pl.
Oct 18|Curly|63033|1313 Mockingbird Lane
...etc...

I need to count the number of lines (or records) in the database and then add the new field in front of the date's array so it looks like the following...

0|Oct 10|Larry|10012|123 Any Street
1|Oct 15|Moe|85224|5 Main Pl.
2|Oct 18|Curly|63033|1313 Mockingbird Lane
...etc...

Here's what I tried to do, but it I'm getting Chunk errors...

Code:
sub add_field {
my $field_cnt1 = "0";

open (DB, "$file") | | die ("Unable to open file $file");
@all = <DB>;
while (@all) {
unshift(@all, "$field_cnt1++");
print DB "$field_cnt1|";
}
close (DB);
}

Maybe there's an easier way to accomplish the same results without having to alter (add) to the existing database?

Also, do I need to use file locking? While one person is deleting their record, that operation won't affect another person wight be adding their record to the same database, at the same time. If so, how?

Thanks!
Quote Reply
Re: Arrays: Adding a new field to the front In reply to
I just realized that adding a number to the front of the record won't work because when a new user adds to the database, the number is currently not being added. The database will be messed up if one script is adding and another isn't.

Oh well, guess I'll just have to add the number when a new record is added and change the existing database to include the number to the first field.

Still would like to know how I can easily change the existing database without having to manually add the number. Any ideas on how to properly use the unshift so I can do this through a script?
Quote Reply
Re: Arrays: Adding a new field to the front In reply to
Always use 'flock' when altering a file.
If all you are trying to do is add an ID field to the beginning of each line you could try something like this:
Code:
open(OF, "$file") or die "blah blah $!\n";
@your_array = <OF>;
close(OF);
open(NF, ">$file") or die "yada yada $!\n";
flock(NF, 2);
for ($i = 0; $your_array; $i++) {
print NF "$i|" . $your_array;
}
close(NF);
flock(NF, 8);

-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);

[This message has been edited by GClemmons (edited November 03, 1999).]
Quote Reply
Re: Arrays: Adding a new field to the front In reply to
In regards to flock, is it absolutely necessary for me to unlock the file by using flock(FH, 8); after I'm finished altering the file's data, or will close(FH); unlock the file for me?

What I'm worried about is, if I'm updating my database and someone else comes along and tries to add to the database, at precisely the same exact time I have the file locked, will this cause a problem or error for that other person? I'd rather avoid such errors.

Thanks again! Smile
Quote Reply
Re: Arrays: Adding a new field to the front In reply to
Perl knows to wait for availability on a flock'ed file. If you had problems with this however you might setup some 'sleep' routine.

Code:
while (!open(F, ">>your.file") && $i < 20) {
sleep(1);
$i++;
}
die "Error: $!\n" if (!undefined(F));
flock(F, 2);
# do whatever with 'F'
close(F);
flock(F, 8);
-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);