Article 7546 of comp.lang.perl: Xref: feenix.metronet.com comp.lang.perl:7546 Newsgroups: comp.lang.perl Path: feenix.metronet.com!news.ecn.bgu.edu!usenet.ins.cwru.edu!eff!news.umbc.edu!europa.eng.gtefsd.com!uunet!pipex!unipalm!ian From: ian@unipalm.co.uk (Ian Phillipps) Subject: Re: wanted: &ctrl('N') that returns "\016" Message-ID: <1993Nov3.131403.6829@unipalm.co.uk> Organization: Unipalm Ltd., 216 Cambridge Science Park, Cambridge CB4 4WA, UK References: <1993Nov2.230719.17093@cirrus.com> Date: Wed, 3 Nov 1993 13:14:03 GMT Lines: 35 dhesi@cirrus.com (Rahul Dhesi) writes: >I am looking for a subroutine for perl 4.019 or 4.036 that will accept >a single alphabetic character, and return a one-character string >containing the corresponding control character. The statement > $ctrl_N = &ctrl('N'); >should have the same effect as: > $ctrl_N = "\016"; Lots of ways. Here's one: sub ctrl { local($_) = $_[0]; tr/A-Za-z/\1-\32\1-\32/; $_; } The local stuff is to avoid the actual parameter being "tr"ed. This is probably the most portable implementation. It will translate whole strings, free of charge. This only does one character, and only upper case: sub ctrl { local($_)=$_[0]; vec($_,6,1)=0; $_; } ... but vec($_,6,2) doesn't do what you might expect. You could even do this: sub ctrl { eval "\"\\c$_[0]\""; } Ian -- Ian Phillipps. Tech support manager, Unipalm. There am three misteaks in Phone +44 223 250103, Fax 250101 this sentence. Pipex phone +44 223 250120. Internic: IP4. Article 7558 of comp.lang.perl: Xref: feenix.metronet.com comp.lang.perl:7558 Path: feenix.metronet.com!news.utdallas.edu!hermes.chpc.utexas.edu!cs.utexas.edu!swrinde!gatech!udel!news.sprintlink.net!news.world.net!teleport.com!kelly!merlyn From: merlyn@ora.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: wanted: &ctrl('N') that returns "\016" Date: 3 Nov 1993 09:01:52 -0800 Organization: Stonehenge Consulting Services; Portland, Oregon, USA Lines: 27 Sender: merlyn@teleport.com Message-ID: References: <1993Nov2.230719.17093@cirrus.com> NNTP-Posting-Host: kelly.teleport.com In-reply-to: dhesi@cirrus.com's message of Tue, 2 Nov 1993 23:07:19 GMT >>>>> In article <1993Nov2.230719.17093@cirrus.com>, dhesi@cirrus.com (Rahul Dhesi) writes: Rahul> I am looking for a subroutine for perl 4.019 or 4.036 that will accept Rahul> a single alphabetic character, and return a one-character string Rahul> containing the corresponding control character. The statement Rahul> $ctrl_N = &ctrl('N'); Rahul> should have the same effect as: Rahul> $ctrl_N = "\016"; sub ctrl { local($[,$tmp) = (0,@_); for (0..(length $tmp)-1) { vec($tmp,$_,8) ^= 32; } $tmp } And that's just *one* way to do it. :-) $x = "Just another Perl hacker,"; for (0..24) { print pack("C",vec($x,$_,8)); } -- Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095 Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying Email: Snail/FAX: (Call) DON'T REPLY TO: merlyn@teleport.com Phrase: "Welcome to Portland, Oregon ... home of the California Raisins!"