?
Solved

Format of cgi script

Posted on 2003-03-14
16
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 84

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

770 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