?
Solved

Format of cgi script

Posted on 2003-03-14
16
Medium Priority
?
210 Views
Last Modified: 2013-11-18
I have perl 5 using a cgi script that send me information from my text box on my web form. The text is intered in my html page as:
category= tree grass lumber etc.in one line.
I need formating in my perl script that when cgi emailes me using the send mail it will be formated as
category tree+grass+lumber
I need formatting code so sendmail will ad the + signes.
0
Comment
Question by:mrwebmaster
  • 5
  • 4
  • 3
  • +2
16 Comments
 
LVL 1

Expert Comment

by:OKSD
ID: 8139912
Quite easy, use the following line

$catagory =~ / /+/;

That changes all spaces into +'s

Replace $catagory with whatever variable your information is storred in.

OKSD
0
 
LVL 1

Expert Comment

by:OKSD
ID: 8139955
Wait, sorry, I did that wrong, the correct line is:

$catagory =~ s/ /+/g;

Stupid me :P

OKSD
0
 
LVL 1

Expert Comment

by:strAtEdgE
ID: 8140024
No, he did it wrong twice, the correct line is:

$category =~ s/\s+/\+/g;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:OKSD
ID: 8140035
No, $catagory =~ s/ /+/g; should work.... what'd I do wrong?

OKSD
0
 
LVL 1

Expert Comment

by:OKSD
ID: 8140037
No, $catagory =~ s/ /+/g; should work.... what'd I do wrong?

OKSD
0
 
LVL 48

Accepted Solution

by:
Tintin earned 200 total points
ID: 8140063
Actually OKSD's second answer is correct if there are single spaces between the words, however a the more efficient way is:

$category =~ tr/ /+/;

or if there are one or more spaces, use

$category =~ s/\s+/+/g;

Note that there is no need to escape the + as it doesn't mean anything special on its own.

0
 
LVL 1

Expert Comment

by:OKSD
ID: 8140579
What's the \s for?

OKSD
0
 
LVL 1

Expert Comment

by:strAtEdgE
ID: 8141194
Tintin: thanks for cutting and pasting my answer into your own. Good job.

OKSD: for starters you spelled category wrong, but also you have no space in your regex therefore it won't find a space (although I wonder if the site here might be what did that).

A \s is better, that picks up any white space, so it includes tabs and sequences of spaces, etc.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8141612
OKSD's regex does have a space in it, although perhaps not obvious depending on what sort of font you view it in.

0
 

Author Comment

by:mrwebmaster
ID: 8141971
After looking at what needs to be accomplished, I may have not made myself clear. This is of no fault of yours it is strictly mine. ( hope format turns out correct )
It has to do with URL encoding
My html file submits from 4 text boxes:
TLC
The Learning Channel
CTLC.com
Reality TV
etc

In my mail to area of my form NOW I have theselines:
Print Mail
  "   KEY4=$in{'key1'}\n",
  "   KEY5=$in{'key2'}\n",
  "   KEY6=$in{'key3'}\n",
  "   KEY6=$in{'key4'}\n",
etc
I am wanting the end format to be:
TLC%2CThe+Learning+Channel%2CTLC.com%2CReality+TV%2C

I need this in place of Key 1,2,3,4 as it is now. I am changing a cgi file, I am not a perl programmer. But I was reading about URL encoded, but I do not know how to use it.
Please Advise


0
 
LVL 1

Expert Comment

by:strAtEdgE
ID: 8144995
First, replace the spaces with + as you wanted and as I have already told you... then join them, like this:

for(1..4) {
 $in{key$_} =~ s/\s+/\+/;
}
print join("%2C", $in{key1}, $in{key2}, $in{key3}, $in{key4});
0
 
LVL 85

Expert Comment

by:ozo
ID: 8145153
for(key1..key4) {
  $in{$_} =~ s/\s+/+/g;
}
#or
for( values %n ){ s/\s/+/g; }
print join("%2C", @in{key1..key4});
0
 

Author Comment

by:mrwebmaster
ID: 8153047
I still cannot get it to work, I have paster the whole code. Can you help me ?( hope my cut and paste works OK The bottom 2 lines is where I need the TLC%2CThe+Learning+Channel%2CTLC.com%2CReality+TV%2C

sub write {
   local ($prefix);

   $time = time;

   $file = $data_dir . 'testing.csv';
   (-e "$file") ? ($prefix = '>>') : ($prefix = '>');

   open(WRITE, "$prefix$file") || &error("Error opening
$file for writing");

   print WRITE join(',', $time, $in{'PROFILE_NAME'},
                         $in{'MAIN_CATEGORY'},$in
{'PROFILE_NAME'},$in{'KEYWORDS'},
               $in{'key1'}, $in{'key2'}, $in{'key3'}, $in{'key4'},
                         $in{'key5'}, $in{'key6'}, $in{'key7'}, $in{'key8'}, $in{'key9'},
                         $in{'key10'}, $in{'key11'}, $in{'key12'}, $in{'key13'}, $in{'key14'},
                         $in{'key15'}, "1|$in{'key16'}", "1|$in{'key17'}", $in{'key18'},

   close(WRITE);

   chmod(0777, $file);
}


sub mail_admin {
   open (MAIL, "|$mail_prog") || &error('Error opening mail connection');
   print MAIL "   PROFILE_NAME=$in{'PROFILE_NAME'}\n",
              "   KEYWORDS=$in{'key1'},$in{'key2'},$in{'key3'},$in{'key4'})\n",
 }
0
 

Author Comment

by:mrwebmaster
ID: 8156743
Got it:
It was by default at:$val =~ s/\,//g;
I have changed it to:$val =~ s/\s+/+/g;

Works great
Thanks
0
 

Author Comment

by:mrwebmaster
ID: 8156745
OOps, forgot to grade it
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8156794
On an unrelated point, where you have:

(-e "$file") ? ($prefix = '>>') : ($prefix = '>');

  open(WRITE, "$prefix$file") || &error("Error opening
$file for writing");


you can just do

open WRITE, ">>$file" or &error("Error open $file for writing $!");
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question