Current location: Hot Scripts Forums » Programming Languages » Perl » write to beginning of file or reverse an array of binary data


write to beginning of file or reverse an array of binary data

Reply
  #1 (permalink)  
Old 01-20-04, 03: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";
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-20-04, 11:09 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Hi abtimoteo,

Quote:
Originally Posted by abtimoteo
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.
a: Why are you using binary for this?
b: See comments below on modules.

Quote:
Originally Posted by abtimoteo
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 ...
See the reverse pod

Code:
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);
You really should be using the CGI.pm module to parse your input. Unless you are using a very old version of Perl this is a standard module.

Code:
# 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";[/QUOTE]
If you structured your data (i.e. tab or | deliminated) it would be easier to m/(add|edit|delete|sort)/; when it comes time to work with it:

Code:
year|month|day|time|Tite of new|artice text|posted by|whatever else|yadayadayada
Then you can do just about anything you want with it at that point.

~Charlie
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM
Write form data to file dragge PHP 1 12-27-03 08:26 PM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 05:28 PM
Upload file type and size limiter! Arctic ASP 1 08-02-03 08:06 PM


All times are GMT -5. The time now is 01:35 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.