Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Beginner writing a text comparison cgi

1 view
Skip to first unread message

Steev...@gmail.com

unread,
Dec 14, 2006, 7:09:07 PM12/14/06
to
Hi
I have a very limited knowledge of perl so forgive me if this is a
stupid question or if it seems very basic. I'm trying to write code
that takes two texts submitted from an html form and compares them and
then displays the following. How may lines the texts have in common and
also lists each word in the texts as well as how may times they appear
in each text.

as i say I'm just a beginner so i hope this makes sense!

Cheers S

Steev...@gmail.com

unread,
Dec 14, 2006, 7:11:13 PM12/14/06
to

SteevWat...@gmail.com wrote:


Sorry i forgot to say i dont have a clue where to start. can somone
cive me some pointers or some code to work with?

Message has been deleted

Gunnar Hjalmarsson

unread,
Dec 14, 2006, 8:23:57 PM12/14/06
to
Steev...@gmail.com wrote:
> I'm trying to write code that takes two texts submitted from an html
> form and compares them and then displays the following....

That guy is multi-posting: http://www.thescripts.com/forum/thread577410.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Tad McClellan

unread,
Dec 14, 2006, 8:03:51 PM12/14/06
to
Steev...@gmail.com <Steev...@gmail.com> wrote:
> SteevWat...@gmail.com wrote:


>> I'm trying to write code


If you show us what you have so far, we can help you fix and extend it.


>> that takes two texts submitted from an html form

[snip]

> Sorry i forgot to say i dont have a clue where to start.


Start with writing a program that can get 2 texts submitted from an html form.

Then write a program that can compare the 2 texts taken from the form.

Then write a program that counts when the texts compare equally.

Then...


> can somone
> cive me some pointers or some code to work with?

Here is the first program mentioned above (untested):

use CGI qw/:standard/;
my $text1 = param('text1');
my $text2 = param('text2');
print "I have gotten '$text1' and '$text2'\n";

Here is the second one:

use CGI qw/:standard/;
my $text1 = param('text1');
my $text2 = param('text2');
print "'$text1' and '$text2' are equal\n" if $text1 eq $text2;


You almost certainly could have gotten that far had you tried...


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

Steev...@gmail.com

unread,
Dec 14, 2006, 8:49:21 PM12/14/06
to

Gunnar Hjalmarsson wrote:

Sorry I didnt realise this war rude.

Steev...@gmail.com

unread,
Dec 14, 2006, 8:52:38 PM12/14/06
to

Tad McClellan wrote:


Thanks I do have some code and its simmilar to yours but mine wont run.
Im posting from my phone on the way home so i'll post my code tomorrow
morning aswell as try yours.

cheers c

Steev...@gmail.com

unread,
Dec 14, 2006, 8:52:42 PM12/14/06
to

Tad McClellan wrote:

Andrew DeFaria

unread,
Dec 14, 2006, 11:41:53 PM12/14/06
to
Here is the first program mentioned above (untested):

use CGI qw/:standard/;
my $text1 = param('text1');
my $text2 = param('text2');
print "I have gotten '$text1' and '$text2'\n";


Here is the second one:

use CGI qw/:standard/;
my $text1 = param('text1');
my $text2 = param('text2');
print "'$text1' and '$text2' are equal\n" if $text1 eq $text2;


You almost certainly could have gotten that far had you tried...
Thanks I do have some code and its simmilar to yours but mine wont run. Im posting from my phone on the way home so i'll post my code tomorrow morning aswell as try yours.
A couple of things the above leaves out. Assuming you're running this on a web server...
  • You need to first put out the HTML headers. Failure to do so will result in an error in the web server's log file. Read about CGI::header.
  • The param names "text1" and "text2" have to match the names of the <INPUT> fields.
  • The web server must be configured to recognize that your Perl script is supposed to be executed. Often people configure the "CGI type" for Apache. If you're not the web admin then ask him about this.
  • The script must be readable and executable by the user that the web server runs as, typically "apache" for the Apache web server. Again, if you're not the admin or confused then ask your web admin.
  • CGI has support for command line execution and debugging. If you're not familiar with the Perl debugger I highly recommend you become best friends with it. It can show you/teach you so much about Perl and programming. Assuming the above is kept in a file named foo.cgi you can do the following: perl -d foo.cgi text1="Hello world" text2=How are you". A short transcript from the Perl debugger follows...
Adding on the -debug pragma I have:
use CGI qw/:standard -debug/;

my $text1 = param('text1');
my $text2 = param('text2');
print "I have gotten '$text1' and '$text2'\n";
Now the debugging session:
perl -d foo.cgi "text1=Hello World" "text2=How are you"

Loading DB routines from perl5db.pl version 1.27
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(foo.cgi:2):      my $text1 = param('text1');
  DB<1> n
main::(foo.cgi:3):      my $text2 = param('text2');
  DB<1> p $text1
Hello World
  DB<2> p $text2

  DB<3> x $text2
0  undef
  DB<4> n
main::(foo.cgi:4):      print "I have gotten '$text1' and '$text2'\n";
  DB<4> x $text2
0  'How are you'
  DB<5> c
I have gotten 'Hello World' and 'How are you'
Debugged program terminated.  Use q to quit or R to restart,
  use O inhibit_exit to avoid stopping after program termination,
  h q, h R or h O to get additional info. 
  DB<5> q
The "n" command tells the Perl debugger to "step over" the next Perl statement. We do this because if we did "s" for step we'd step into the param call and be debugging the CGI.pm module. You probably don't want to do that! Next the "p" command prints a variable. As you can see $text1 is now set to "Hello World". If we p $text2 nothing comes out. Because we haven't stepped over the second param call thus $text2 is undefined at this moment. The "x" command prints out data structures like hashes and arrays. Additionally it prints "undef" if the variable is undefined. Next we step over (n command) the second param call then examine $text2 using the "x" command this time. Finally we "c" continue ending the program.

For more on Perl debugging try perldoc perldebtut and perldoc perldebugger
-- 
Andrew DeFaria
Why is a person who plays the piano called a pianist, but a person who drives a race car not called a racist?

Mumia W. (on aioe)

unread,
Dec 15, 2006, 2:01:37 AM12/15/06
to
On 12/14/2006 07:23 PM, Gunnar Hjalmarsson wrote:
> Steev...@gmail.com wrote:
>> I'm trying to write code that takes two texts submitted from an html
>> form and compares them and then displays the following....
>
> That guy is multi-posting:
> http://www.thescripts.com/forum/thread577410.html
>

Thescripts.com is a completely different forum from clpm. There is no
reason to suspect that a significant portion of readers of clpm also
read thescripts.com.

Asking a question on both clpm and thescripts.com is like asking a
lawyer for an opinion then getting a second opinion from another lawyer.

I can see where some people think that posting the same thing on usenet
twice in different groups is rude, but saying that someone can't post
the same thing in two different places anywhere on the Internet doesn't
make any sense to me.


--
paduille.4...@earthlink.net
http://home.earthlink.net/~mumia.w.18.spam/

Michele Dondi

unread,
Dec 15, 2006, 5:20:11 AM12/15/06
to
rOn Fri, 15 Dec 2006 01:01:37 -0600, "Mumia W. (on aioe)"
<paduille.4...@earthlink.net> wrote:

>Asking a question on both clpm and thescripts.com is like asking a
>lawyer for an opinion then getting a second opinion from another lawyer.

Yet it would be fine to point out having done so, and possibly report
to any of the resources interesting info got from the other one...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Gunnar Hjalmarsson

unread,
Dec 15, 2006, 1:04:08 PM12/15/06
to
Mumia W. (on aioe) wrote:
> On 12/14/2006 07:23 PM, Gunnar Hjalmarsson wrote:
>> That guy is multi-posting:
>> http://www.thescripts.com/forum/thread577410.html
>
> Thescripts.com is a completely different forum from clpm. There is no
> reason to suspect that a significant portion of readers of clpm also
> read thescripts.com.

I agree. But how would that justify multi-posting? On the contrary, just
that fact increases the risk that somebody here spend time on an answer
without knowing that the problem already was resolved over there (or
vice versa).

The OP's explanation at thescripts.com:
"I posted in multiple forms because different people visit different
forums and I want as much help as i can get."
reveals an inconsiderate view on how to (ab)use the free resources for
seeking help with Perl.

> Asking a question on both clpm and thescripts.com is like asking a
> lawyer for an opinion then getting a second opinion from another lawyer.

There is a huge difference: The person who asks two lawyers for advice
get two bills, i.e. s/he pays for the services.

> I can see where some people think that posting the same thing on usenet
> twice in different groups is rude, but saying that someone can't post
> the same thing in two different places anywhere on the Internet doesn't
> make any sense to me.

Please allow me to disagree. The OP took the point and apologised, so I
was kind of surprised to see you defend him. ;-)

brian d foy

unread,
Dec 16, 2006, 5:03:21 PM12/16/06
to
In article <1166147361....@16g2000cwy.googlegroups.com>,
<Steev...@gmail.com> wrote:

Usually it's enough to just say where you've also asked this question
and to provide a link to it. That way someone doesn't spend a lot of
time typing an answer that you've already received elsewhere.

To be extra nice, you can go back to the places you posted and provide
links to the answers you got elsewhere.

Good luck :)

--
Posted via a free Usenet account from http://www.teranews.com

0 new messages