CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Add more data to flatfile db

 



spaz
Novice

Nov 9, 2000, 1:10 PM

Post #1 of 10 (1293 views)
Add more data to flatfile db Can't Post

I have a flatfile which looks something like this:

0|name|date|color
1|bill|11/29/99|blue
2|bob|11/30/99|red

I want to add a field to some of the lines aif selected from a form. For example: 1|bill|11/29/99|blue|yes

This will only happen on certain occasions, where I can use the last field as a field when displaying info. I'm trying to use push, but it's just not happening.


sleuth
Enthusiast / Moderator

Nov 9, 2000, 1:51 PM

Post #2 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

 I wrote some code out at this post
http://www.perlguru.com/forum/Forum7/HTML/000435.shtml

And I explain how to delete a line and re-add it in the same position, mainly this is for line editing, but you should be able to swing it to do what you want.

What you should do is when you change the line that uses push(); the first time, just add the new field at the end of the line.

Sleuth


sleuth
Enthusiast / Moderator

Nov 9, 2000, 10:55 PM

Post #3 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

 
Bingo, yea, use hidden values so that you can identify what line the information they are about to add to. Store origional field values in the hidden tags and then use those to find the line, then add the whole line over again in that first push statement.

Sleuth


spaz
Novice

Nov 10, 2000, 5:16 AM

Post #4 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

Thanks -- do I need to pass all of the information again in the form in hidden tags? For example, I can pass the ID to match it, but do I need to pass along all of the other variables, too, so they will all be included in the new line -- this would be values for $form{'field1'}, etc. in your code.


spaz
Novice

Nov 10, 2000, 11:12 AM

Post #5 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

I think I'm getting close, except for the fact that it's not rewriting the file. In fact, the else statement actually wiped out my database -- go figure. If I print @new in a return, I'm getting the right line of pipe-delimited info. The replacement just isn't happening. Does it have something to do with the "print new_data @new;" The permissions look OK. Here is my code:

$datafile = "./backup.txt";
open(DATA, "<$datafile");
while(<DATA> )
{
@Temp = split(/|/, $_);

if (($Temp[12] == "$idnumber") && ($Temp[13] eq "$iddate"))
{
push(@new, "$0|$1|$2|$3|$4|$5|$6|$7|$8|$9|$10|$11|$idnumber|$iddate|$14\n");
}
else
{
push(@new, $_);
}
}
close(DATA);
open(new_data, ">$datafile");
print new_data @new;
close(new_data);


sleuth
Enthusiast / Moderator

Nov 10, 2000, 11:58 AM

Post #6 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

 
Just this,

@Temp = split(/|/, $_);

Should be

@Temp = split(/\|/, $_);

| means or remember? So it doesn't read it as a pipe unless your escape it.

Try that,

Sleuth


spaz
Novice

Nov 10, 2000, 12:11 PM

Post #7 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

Good catch Sleuth -- it's still not working. I tried to print @new to see what it holds and the array is basically empty. I did another check and the variables are all coming through though.

I've even tried removing the if else statement and there is still nothing there. Do I need a foreach loop instead of the WHILE. Would that help?


sleuth
Enthusiast / Moderator

Nov 10, 2000, 1:53 PM

Post #8 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

 
No, foreach is the same really,

Ok, try this, Change

open(DATA, "<$datafile");

To

open(DATA, "<$datafile") &#0124; &#0124; print "$!";

That way, you can see if an error is occuring wile trying to read the data base, since the @new is turning out empty, that says it's not reading the data base, with an else satement in their it would throw all the data into @new if something was wrong with the if(). Know what I mean. Also, good thinking by printing out @new to see if it was even anything. Is the persmission of the data base your reading 777?

Sleuth


sleuth
Enthusiast / Moderator

Nov 12, 2000, 10:38 PM

Post #9 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

 Great, That $! is a life saver! he he,

Sleuth


spaz
Novice

Nov 13, 2000, 4:28 AM

Post #10 of 10 (1293 views)
Re: Add more data to flatfile db [In reply to] Can't Post

That did the trick! Thanks Sleuth. The path I had wasn't opening up the right file. I actually had it set to another variable of the path, so it cancelled out and tried to open the variable name instead of the associated path.

Everything is working great. Thanks for the help.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives