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

Form Parsing and File reading trouble. :(

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

I had thrown together a small script which would take the number a person would enter into a form and open up the corresponding file and spit it out. The files are .WRI files, and I had it working about a year ago and have lost it since. Now trying to duplicate what I did then, I find myself pulling my hair out. At one point I had it reading the first line, but nothing else, now it's at the point where I get an internal server error, and I'm ready to go get an asp book and start learning that. lol Anyways, here is the current code, any help would be greatlty appreciated.

#!/usr/local/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

if ($ENV{'QUERY_STRING'}) {
@getpairs =split(/&/, $ENV{'QUERY_STRING'});
push(@pairs,@getpairs);
}
} else {
print "Content-type: text/html\n\n";
print &quot;<P>Use Post or Get&quot;;
}

foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;

$value =~s/<--(.|\n)*-->//g;

if ($formdata{$key}) {
$formdata{$key} .= &quot;, $value&quot;;
} else {
$formdata{$key} = $value;
}
}


open(MYFILE, &quot;<$formdata{$key}.WRI&quot;);
my(@lines) = <MYFILE>;
my($line);
foreach $line (@lines)
{
$displayfile=<MYFILE>;
}

print &quot;Content-type: text/html\n\n&quot;;
foreach $key (sort keys(%formdata)) {
print &quot;<P><B>$displayfile</B>&quot;;
}
 
Okay.. follows is some revamped code broken into sections with some comments attached...

First off, this sounds like something you could do just using the get method, so it doesn't seem necessary to put all the post stuff in there (what method are you using on your form? Use that and that alone). Also, if you're just using one form field, splitting on the '&' symbol isn't necessary...And if it's a number you're looking for, there probably aren't going to be any hex encoded characters, so all the pack stuff isn't necessary either. The first section can thus be reduced to:
Code:
#!/usr/bin/perl

@pairs = split /=/,$ENV{'QUERY_STRING'};
die if $pairs[1] =~ /[^\d]/ || $pairs[1] == undef;
Now on to the open file stuff. The best way to do this is to open the file, read the filehandle into an array, close the filehandle and simply print the array. So the whole code becomes:
Code:
#!/usr/bin/perl

@pairs = split /=/,$ENV{'QUERY_STRING'};
die if $pairs[1] =~ /[^\d]/ || $pairs[1] == undef;

open(MYFILE,&quot;$pairs[1].WRI&quot;) || die;
@lines = <MYFILE>;
close(MYFILE);

print &quot;Content-type: text/html\r\n\r\n&quot;;
print &quot;<pre>@lines</pre>&quot;;
Of course, this isn't tested, but it looks like it should work.

Hope this helps,

brendanc@icehouse.net
 
Thanks a bunch Brendan, one thing I forgot to mention was that the numbers for some of the names have hyphens.

ie: 1-001 etc.

How would the code change to accomodate this?

I tried the code you gave me, it didn't work at first then I tied it again renaming the .WRI file and taking out the hyphen, and it worked perfect, even better than the first time I got it running, but that wouldn't take much considering my abilities. hehe But the majority of the 600 files I'm uploading have hyphens, and I'd like to avoid renaming them if possible.

Thanks again for your time, also is there a web site you have that I can give credit to on our site as well ?
 
Just change the regular expression at the top from /[^\d]/ (which actually should have been written as /\D/, for future reference) to /[^\d\-]/. That way it'll pass through digits and hyphens and die on anything else.

My site is .. but it hasn't been updated in nearly a year. :) I'm doing a redesign of it and it should be back with quite a bit more content in regards to programming and design sometime in June.

brendanc@icehouse.net
 
Hey Brenedan, I like your site :), especially the production notes on the release of Sophie, very cool.

I have one last question about the script, you've been a huge help and saved me a lot of time and hair, and I hope this will be the last one. ;)

I added html code to the mix, it works fine, but doesn't return at the end of the table, seems to ignore all the table tags, and the text formatting seems to be a bit off, but it's the returning that's a bit of a downer. I set it up to disply in a table 550 wide, and also 95% but both the same, they display tables about 2000 pixels wide. lol Anyway to correct this ? So that the html tags affect the info a bit more?

Once again thanks for everything.
 
Hair is good. :) Glad to be of some help.

Anyway, the reason you're getting awkward formatting is the <pre> tag. That pretty much escapes all the html constraints you might construct and instead, formats the text as it would appear in a typical text editor. So, you can replace those tags with <p> or nothing at all and have the text restricted to the region you code in, however, all line breaks and multiple spaces will be reduced to one space as per the HTML standards. You may be able to avoid this though... at least somewhat. It will, unfortunately, make the code's return a bit baggy, but what can ya do? The idea is to replace all line breaks in the text with the <br> tag, and replace multiple instances of spaces with non-breaking space characters (&nbsp; ). To do that, we need to put the file into a scalar variable and run a couple of regular expressions on it. So the code that opens and prints the file looks like:
Code:
open(MYFILE,&quot;$pairs[1].WRI&quot;) || die;
$lines = join '',<MYFILE>;
close(MYFILE);
$lines =~ s/[\r\n]/<br>/g;
$lines =~ s/\s(?=\s)/&nbsp\;/g;
print &quot;Content-type: text/html\r\n\r\n&quot;;
print &quot;<p>$lines</p>&quot;;

Hope this helps,

brendanc@icehouse.net
 
Ack. I just realized that Tek-tips read the non-breaking space symbol as a whitespace character rather than the actual text. Well, the symbol is an ampersand (shift-7, aka the &quot;and&quot; symbol) followed by the letters nbsp and a semicolon. So, in the line that says:
Code:
$lines =~ s/\s(?=\s)/ \;/g;
Make that:
Code:
$lines =~ s/\s(?=\s)/nbsp\;/g;
with an ampersand preceeding the nbsp. :sigh: Looked fine in the preview window.

brendanc@icehouse.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top