Link to home
Start Free TrialLog in
Avatar of mrwebmaster
mrwebmaster

asked on

Format of cgi script

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.
Avatar of OKSD
OKSD

Quite easy, use the following line

$catagory =~ / /+/;

That changes all spaces into +'s

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

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

$catagory =~ s/ /+/g;

Stupid me :P

OKSD
No, he did it wrong twice, the correct line is:

$category =~ s/\s+/\+/g;
No, $catagory =~ s/ /+/g; should work.... what'd I do wrong?

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

OKSD
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What's the \s for?

OKSD
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.
OKSD's regex does have a space in it, although perhaps not obvious depending on what sort of font you view it in.

Avatar of mrwebmaster

ASKER

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


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});
for(key1..key4) {
  $in{$_} =~ s/\s+/+/g;
}
#or
for( values %n ){ s/\s/+/g; }
print join("%2C", @in{key1..key4});
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",
 }
Got it:
It was by default at:$val =~ s/\,//g;
I have changed it to:$val =~ s/\s+/+/g;

Works great
Thanks
OOps, forgot to grade it
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 $!");