CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Redundant code



Jul 2, 2000, 5:54 PM

Post #1 of 4 (1610 views)
Redundant code Can't Post


the following code should take a QUERY STRING and seperate it by the '&' seperators...

@DATA = split(/&/,$ENV{'QUERY_STRING'});
foreach (@DATA) {
($variable1, $variable2) = split(/&/,$ENV{'QUERY_STRING'});

So if you ran this string would
basically do this:
$variable1 = "fo";
$variable2 = "fun";

It works just fine, just looks redundant. multiple 'split's anyone think of a streamlined way to do this?


Jul 2, 2000, 7:00 PM

Post #2 of 4 (1610 views)
Re: Redundant code [In reply to] Can't Post

I think you have your idea wrong. Your code is quite redundant. The QUERY_STRING is sent in the form FIELD=VALUE&FIELD=VALUE&... If there is nothing between the = and the &, the value is "". If there is a & immediately after the FIELD, assume a true value (1).

So your code should really be:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@DATA = split /&/, $ENV{QUERY_STRING};
for (@DATA) {
push @FIELDS, (s/([^=]+)//, $1)[1]; # the non = characters
push @VALUES, (s/^=// ? $_ : 1);

If the QUERY_STRING is 'foo=bar&this=&that', then @FIELDS = ('foo','this','that'), and @VALUES = ('bar','',1).

Then again, perhaps is right for you.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use CGI;
$query = new CGI;
$value_of_foo = $query->param('foo');

Note:'s query parsing dictates that a field with no =VALUE associated with it has a value of undef, whereas a field with an =, but no VALUE, as a value of "".


Jul 2, 2000, 7:32 PM

Post #3 of 4 (1610 views)
Re: Redundant code [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

($variable1, $variable2) = split(/&/,$ENV{'QUERY_STRING'});</pre><HR></BLOCKQUOTE>
... should suffice, as what you're doing is resetting the two variables after already setting them. ie,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

foreach( @DATA ) { ... }
# is the same as
foreach ( "fo", "fum" ) { ... }
# is (functionally) the same as
foreach ( 1, 2 ) { ... }
# is the same as

For scalability, tho', I'd stick with the array assignment because you could then access variables as $DATA[0] (fo), $array[1] (fum), etc. but not have to hardcode any more variables if you decided to pass more arguments.

It's odd to see arguments like that passed to a script though. Usually they take a key/value form like ?colour=blue&indent=yes.

I suspect that's where you got your redundant loop from as there's lots of code out there like ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@pairs = split(/&/,$ENV{'QUERY_STRING'});
foreach $pair (@pair) {
($key,$value) = split(/=/,$pair);
$inpput{$key} = $value;

(Note the second split on = not &).

However, is this is what you were really after, I'd advise you to use or one of the other CGI modules instead. Makes life a lot easier and saves you reinventing the wheel.
If that's what you had in mind, then you should take

[This message has been edited by Kanji (edited 07-02-2000).]


Jul 2, 2000, 9:41 PM

Post #4 of 4 (1610 views)
Re: Redundant code [In reply to] Can't Post

Thanks Kanji,
Your condensing of my code is so simple, i should've figured it out, but you know how it is you code and code and the simplist strings escape you.
I probably wont use, as the variables only need to be declared once.
thanks again (the new code is already inserted to the program) If anyone cares, the script is located at
and a text file of it at /cgi-bin/netrep.txt


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

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