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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Got tie::file to work, but still having problems...

Status
Not open for further replies.

cradletoenslave

Programmer
Jan 28, 2005
29
US
Well, I got it to work, but it's still not quite what I wanted. I got it write in the middle of the document, but I need it to append to the information that is already there. Think of a guestbook script. This would work, except it writes over the previous entry. What would be a good method to make a guestbook script so that the program appends new information in the middle of the file?

Also, if anybody knows of a good tutorial to stem off from the pageresource.com tutorials, I'd be really grateful. I've gotten most of what was offered on pageresource, but all of the other tutorials are over my head still.
 
Also, with the tie::file, I get a really weird error on the file that I just appended. The server refuses to let me view it. The Chmod's are all okay, in fact I put it on 777 (overkill doesn't hurt at this point). The URL is correct, but the server still won't let me view it. When I look up the file on my file manager, all of the HTML and codes are okay, it just won't let me view it. Weird huh?
 
If you want to add new stuff into the middle of an array but not overwrite any existing data, you probably want to slice the array into two arrays, append the new entry onto the end of the first slice and then rejoin the two array slices back together.

very generic example:

Code:
my @array = (1 .. 100);
my @first_half = @array[0..49];
my @second_half = @array[50..99];
my $new_record = 'test';
push (@first_half, $new_record);
@array = (@first_half,@second_half);
print "@array";
 
It'd be much easier to use the splice() function:
Code:
my @array = (1 .. 100);
my $new_record = 'test';

splice @array, 50, 0, $new_record;

print "@array";

That splice line means to remove 0 elements starting at position 50, replacing them with $new_record, which obviously has the effect of just inserting $new_record into the array. It's like substr for arrays!
 
ahh.... yes, ishnids suggestions is a bit better.
 
Hmmm... I think I need to entirely rewrite my program. So far, these methods would work in other cases, but this one would be very problematic and complicated. I'd better think of a new way of doing things...

What I'm trying to do is create a guestbook script. Yes, I know that there are tons of free ones out there, but I'm too independant to do that. Here's how the program works:

The user fills out an html form and the information is sent off to a cgi data processing file. The processing file has a form processor file as well, so that it can read the name inputs from the html document.

First name:<input type="text" name="fname">

The file processing form reads the "name" field from the html files and assigns them to a variable.

So far, this is what the script kinda looks like:

#!/usr/bin/perl

require "formprocessor.cgi";

&ReadParse(*input);

$fname= $input{'fname'};

use Tie::File;

tie @array, 'Tie::File', 'replies.cgi' or die "oops";

$array[2] = "print \"$fname\"\;";";

untie @array;


_________________

This script is entitled reply.cgi, the script that contains the new data is replies.cgi. What tiefile does is it makes the file(replies.cgi, the file to be changed) into an array by the different lines. So what this does is it prints whatever they typed in on the html document onto the third line of that replies.cgi. Now what I need it to do is keep the information there and whenever somebody else submits information, it will simply add that information on. Splicing an array would work, except I could never control just how many lines of code would be on replies.cgi, creating hella array problems.

Perhaps somebody knows a better way of doing this! I tried to remember the KISS rule, but it seems I kinda blew it.

Also, if anybody knows of some good perl tutorials, that would be great. I've mastered most everything that pageresource.com has to offer, but it's not nearly enough.

Any help is greatly appreciated!
 
I can't figure out what you are trying to do by your description of the script. And this line just looks wrong:

$array[2] = "print \"$fname\"\;";";

should it be:

$array[2] = "print \"$fname\"\;";







 
I can't figure out what you are trying to do by your description of the script. And this line just looks wrong:

$array[2] = "print \"$fname\"\;";";

should it be:

$array[2] = "print \"$fname\"\;";

Yes, thank you. I must have gotten ahead of myself while pasting the information from the original script.

I'll break down my script some more.

Note that this script is dependent on information from the "name" field in the html document.

You may visit the page at
Code:
#!/usr/bin/perl

require "formprocessor.cgi";

#This allows me to call information from the html #document.  I call the information with $input{'whatever #the name is within the html tag'};

&ReadParse(*input);

$fname= $input{'fname'};

use Tie::File;

tie @array, 'Tie::File', 'replies.cgi' or die "oops";

#This part opens replies.cgi and turns the entire file into #an array.  Each line of replies.cgi is an element in the #array

$array[2] = "print \"$fname\"\;";

#This places the string print "$fname";  on the third line #of replies.cgi.

untie @array;

Sooooo....

Information is submitted from the html document. reply.cgi processes the information and posts it onto replies.cgi so that I can view it. Quite like a message script. The only problem is that it will of course overwrite the previous name each time the new information is submitted.
 
hmm.... would simple cocatenation be what you need?

$array[2] .= "print \"$fname\"\;";

note in place of "=" I put ".=" which concatenates (appends to) the exisitng string in $array[2].
 
I think it might. I'm seeing it create future complications though... I'm already seeing the problem that if I need to change something else in the script that is on a different line, it may not work due to the fact that too much information would create two lines, therefor throwing off the count.


replies.cgi

(information from line 3)----------------------------------------------(If too much is submitted, the information would go to the next line down).


Let's say I need something else appended down here. In the above script, this would not be line 4 anymore, but instead line five because of all the information. I think I should do this whole program some other way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top