A question to Perl wizards 
Author Message
 A question to Perl wizards

This should be easy for Perl wizards:  I need to remove special escape codes
in a string, and convert it to its normal ascii representation.  The escape
sequence is triggered by the character `%' (without the `'), and the following
two characters is the hexadecimal value of the character. So, let say I
have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"
(of course, without the quotes).

Any help?  Thanks.

--

 URL http://www.*-*-*.com/ ~lebel |  administrateur du Web au DIRO, UdeM
_
 " Whatever you may think of me, don't ever take my dignity. Whatever you
   may do to me, don't crucify my dignity. " - New Order, _This Time of Night_



Mon, 25 Nov 1996 05:35:43 GMT  
 A question to Perl wizards


Quote:
(David Lebel) writes:
>This should be easy for Perl wizards:  I need to remove special escape codes
>in a string, and convert it to its normal ascii representation.  The escape
>sequence is triggered by the character `%' (without the `'), and the following
>two characters is the hexadecimal value of the character. So, let say I
>have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"

(of course, without the quotes).

Quote:
>Any help?  Thanks.

------------------------
#!/usr/bin/perl
#
#
print "Enter the string with % stuff in it:\n";
$_ = <STDIN>;
chop;

s/%[0-9A-Z]{2}//g;
print "$_\n";
#
# If you want to modify the string in place:
$str = "%28tcsh%7Ccsh%29";
$str =~ s/%[0-9A-Z]{2}//g;
print "$str\n";
#
# If you want to modify the string storing the results elsewhere;
$str = "%28tcsh%7Ccsh%29";
($modified = $str) =~ s/%[0-9A-Z]{2}//g;
print "$modified\n";
-----

Je vous prie de croire a mes salutations les meilleures,
Dave Nadler

--
"Dmitri, you remember that we we're talking about how one day
we might have a problem with the bomb. --No, the bomb, Dmitri,
the hydrogen bomb."



Mon, 25 Nov 1996 12:26:37 GMT  
 A question to Perl wizards

Quote:


>(David Lebel) writes:
>>This should be easy for Perl wizards:  I need to remove special escape codes
>>in a string, and convert it to its normal ascii representation.  The escape
>>sequence is triggered by the character `%' (without the `'), and the following
>>two characters is the hexadecimal value of the character. So, let say I
>>have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"
>(of course, without the quotes).
>>Any help?  Thanks.
>------------------------
>#!/usr/bin/perl
>#
>#
>print "Enter the string with % stuff in it:\n";
>$_ = <STDIN>;
>chop;
>s/%[0-9A-Z]{2}//g;
>print "$_\n";
>#
># If you want to modify the string in place:
>$str = "%28tcsh%7Ccsh%29";
>$str =~ s/%[0-9A-Z]{2}//g;
>print "$str\n";
>#
># If you want to modify the string storing the results elsewhere;
>$str = "%28tcsh%7Ccsh%29";
>($modified = $str) =~ s/%[0-9A-Z]{2}//g;
>print "$modified\n";
>-----

I guess he meant:

$str =~ s/%([0-9a-f]{1,2})/pack(C,hex($1))/eig;

Ciao
Franz



Mon, 25 Nov 1996 15:08:22 GMT  
 A question to Perl wizards

Quote:

>(David Lebel) writes:
>This should be easy for Perl wizards:  I need to remove special escape codes
>in a string, and convert it to its normal ascii representation.  The escape
>sequence is triggered by the character `%' (without the `'), and the following
>two characters is the hexadecimal value of the character. So, let say I
>have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"
>(of course, without the quotes).
>Any help?  Thanks.

My first followup was complete BS and I missed the point.
I think I've got it now.

#!/usr/bin/perl

#
# If I can do it, you don't have to be a wizard.
#
# If you want to modify the string in place:

$_ = "%28tcsh%7Ccsh%29";

s/%[0-9A-F]{2}/sprintf("%c",hex(substr($&,1)))/eg;

print "$_\n";



Mon, 25 Nov 1996 13:11:42 GMT  
 A question to Perl wizards
|>

Quote:
|> (David Lebel) writes:

|>
|> >This should be easy for Perl wizards:  I need to remove special escape codes
|> >in a string, and convert it to its normal ascii representation.  The escape
|> >sequence is triggered by the character `%' (without the `'), and the following
|> >two characters is the hexadecimal value of the character. So, let say I
|> >have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"
|> (of course, without the quotes).

|> # If you want to modify the string in place:
|> $str = "%28tcsh%7Ccsh%29";
|> $str =~ s/%[0-9A-Z]{2}//g;
|> print "$str\n";

Er, this doesn't do what the poster wanted; it simply removes the escape
sequences from the string. Try

$str = "%28tcsh%7Ccsh%29";
$str =~ s/%([0-9A-Z]{2})/sprintf("%c",eval("0x$1"))/gei;
print "$str\n";

--
Philip Hazel                   University Computing Service,




Mon, 25 Nov 1996 18:45:57 GMT  
 A question to Perl wizards

Quote:
> $str = "%28tcsh%7Ccsh%29";
> $str =~ s/%([0-9A-Z]{2})/sprintf("%c",eval("0x$1"))/gei;
> print "$str\n";

I would use:

    $str = "%28tcsh%7Ccsh%29";
    $str =~ s/%([0-9A-Z]{2})/pack("c",hex($1))/gei;
    print "$str\n";

Use hex rather than eval, and pack rather than sprintf.

--




Mon, 25 Nov 1996 19:53:48 GMT  
 A question to Perl wizards

Quote:

>This should be easy for Perl wizards:  I need to remove special escape codes
>in a string, and convert it to its normal ascii representation.  The escape
>sequence is triggered by the character `%' (without the `'), and the following
>two characters is the hexadecimal value of the character. So, let say I
>have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"
>(of course, without the quotes).

s/%(..)/pack("c",hex($1))/eg;

--Malcolm

--

Oxford University Computing Services
"Widget. It's got a widget. A lovely widget. A widget it has got." --Jack Dee



Mon, 25 Nov 1996 19:20:48 GMT  
 A question to Perl wizards

|> This should be easy for Perl wizards:  I need to remove special escape codes
|> in a string, and convert it to its normal ascii representation.  The escape
|> sequence is triggered by the character `%' (without the `'), and the following
|> two characters is the hexadecimal value of the character. So, let say I
|> have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"
|> (of course, without the quotes).

It seems like you are trying to convert cgi variables, this is the demo
code supplied with NCSA httpd:

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

--

Michael Salmon

#include        <standard.disclaimer>
#include        <witty.saying>
#include        <fancy.pseudo.graphics>

Ericsson Telecom AB
Stockholm



Tue, 26 Nov 1996 15:01:49 GMT  
 A question to Perl wizards

Quote:

>This should be easy for Perl wizards:  I need to remove special escape codes
>in a string, and convert it to its normal ascii representation.  The escape
>sequence is triggered by the character `%' (without the `'), and the following
>two characters is the hexadecimal value of the character. So, let say I
>have this string:  "%28tcsh%7Ccsh%29", it should translate into: "(tcsh|csh)"
>(of course, without the quotes).

  #!/usr/local/bin/perl
  $x = "%28tcsh%7Ccsh%29";
  $x =~ s/\%(..)/pack('H2',$1)/eg;
  print "'$x'\n";

------------------
--
Dr. Peter G. Ford
Operations Manager, Geosciences Node
NASA Planetary Data System



Wed, 27 Nov 1996 11:39:27 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Answers to questions the Perl wizards won't answer!

2. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

3. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

4. easy XS question for a wizard from a newbie

5. A call to PERL wizards

6. One for the Perl Wizards...

7. Perl Wizard Quiz

8. Perl for Netware wizards out there?

9. Any CGI/Perl Wizards in Utah?

10. Perl Wizards

11. HELP: (please help, wizards) Memory problems

 

 
Powered by phpBB® Forum Software