View Single Post
  #1 (permalink)  
Old 01-20-04, 02:11 AM
abtimoteo abtimoteo is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Question write to beginning of file or reverse an array of binary data

i have a flat-file news database and i expect it grow - that is, into dozens and hopefully hundreds of news submissions.

i want to do either of two things to accomplish one objective - TO SEND THE FLAT-FILE NEWS DATABASE TO STDOUT WITH THE LATEST NEWS SUBMISSION AT THE TOP.

option 1: write each new submission to the beginning of the flat file. how can this be done under the following conditions?

a. my data is in binary mode.
b. a perl module is out of the question because of the restrictions imposed by my web host.

option 2: reverse the array of news submissions - again, under conditions a and b above, before it is written to the file. i have tried doing it several times but ...

The script to parse form inputs and write binary data to the flat file:

# PARSE FORM INPUTS.

read (STDIN, $FormInputs, $ENV{'CONTENT_LENGTH'});
@NameValuePairs = split (/&/, $FormInputs);
for ($i = 0; $NameValuePairs[$i]; $i++)
{
($Name, $Value) = split(/=/, $NameValuePairs[$i]);
$Name =~ tr/+/ /;
$Value =~ tr/+/ /;
$Name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$Value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Convert newlines to linebreaks.
$Value =~ s/\n/<br>/g;
$NameValuePairs[$i] = $Value;
}

$ThisNewsSubmission = pack ("a50a50a150a2500l" , @NameValuePairs);

# WRITE TO A FLAT FILE.

# Open the flat file named "xyzNewsSubmissions.dat" for appending.
# Create and upload the file manually.

open (NEWSSUBMISSIONS, ">>xyzNewsSubmissions.dat")
|| die "Could not open file: $!\n";

# Select NEWSSUBMISSIONS as output device.

select (NEWSSUBMISSIONS);

print NEWSSUBMISSIONS $ThisNewsSubmission;

# Close the file.

close (NEWSSUBMISSIONS)
|| die "Could not close file: $!\n";
Reply With Quote