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: General Discussions: General Questions:
File contents

 



obrien135
Novice

Nov 23, 2012, 5:42 AM

Post #1 of 5 (4851 views)
File contents Can't Post

Hello,



The question I have today is:



I have been writing data into a file from an html form where the action is performed by selecting a submit button. I thought I would get seperate entries into the file but when I try to read them out into an array it performs as though it is one big string. I am wondering why this is happening and how I can getthem to be seperate elements in an array.



Thank you.



George



#!/usr/bin/perl
print "Content-type: text/html\n\n";


$n = 0;
$k = 0;
@array = ();
$Lineout = "";
$TheReq = "";
if($ENV{"REQUEST_METHOD"} eq 'GET'){$TheReq = $ENV{"QUERY_STRING"}}
else
{read(STDIN, $TheReq, $ENV{"CONTENT_LENGTH"})}
#print $TheReq;
open(OUT, '>>file.txt') or die "Couldn't open file.txt file, \n";
#select(OUT);
printf OUT "<HTML>%s</HTML>", $TheReq;
#select(STDOUT);
#print("hello world2");
close(OUT);
$TheReq = "";


#print("hello world2");
open(B, '<file.txt');

$n = -1;
while (<B>) {
chomp;
$n++;
$Lineout = $_;
@array[$n] = $Lineout;

#printf ("<HTML><P>%d<BR></P></HTML>", $n);


#print $n;
}
# print("hello world 2.5");

#print("hello world3.5");
close (B);


#print("hello world6");


#$k = $n;

$k = 0;
#printf ("<HTML><P>%d<BR></P></HTML>", $n);

#while($k >= 0)
#{
#$k -= 1
#printf ("<HTML><P>%s<BR></P></HTML>", @array[$k]);
#}
for($k == $n; $k >= 0; $k--)
{
printf ("<HTML><P><BR>%s<BR></P></HTML>", @array[$k]);
#printf ("<HTML><P>%d<BR></P></HTML>", $k);
}
#print("hello world6.5");


FishMonger
Veteran / Moderator

Nov 23, 2012, 6:27 AM

Post #2 of 5 (4850 views)
Re: [obrien135] File contents [In reply to] Can't Post

The answer is the same as the one for your other question.

Use the CGI module.

http://search.cpan.org/~markstos/CGI.pm-3.63/lib/CGI.pm


obrien135
Novice

Nov 23, 2012, 12:04 PM

Post #3 of 5 (4843 views)
Re: [FishMonger] File contents [In reply to] Can't Post

Thank you for that info FishMonger. I went to that website and the nearest thing to what I need seems to be



$values = $query->param('foo');

-or-

$value = $query->param('foo');



But I am not sure how to use it. Can it help me parse out the + signs? Is foo a variable? How do I make it work with the code I posted?


obrien135
Novice

Nov 24, 2012, 3:29 AM

Post #4 of 5 (4831 views)
Re: [obrien135] File contents [In reply to] Can't Post

Thanks, I found a way to replace the special text. But as of yet I haven't been able to get the reverse() function to do anything. Is there any other method?


Laurent_R
Enthusiast / Moderator

Nov 24, 2012, 4:06 AM

Post #5 of 5 (4827 views)
Re: [obrien135] File contents [In reply to] Can't Post

What do you want to do with the reverse function? There is nothing really mysterious about this function, which returns an array sorted in reversed order compared to the original order.

This is a Perl one-liner showing the use of reverse:


Code
$ perl -e '@c="a".."g"; print "@c \n"; @d = reverse @c; print "@d \n";' 
a b c d e f g
g f e d c b a


 
 


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

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