markguy has asked for the wisdom of the Perl Monks concerning the following question:

(Perhaps the title does not make sense... please bear with me)

Short version of a much longer problem: I'd like to print out a list of all the special characters that can be represented by "\277" or "CNTL-q-2-7-7" if you use emacs. In particular, I'm looking for a clever way to mess with those damn Windows smart quotes, but there are other concerns as well.

So I tried variations of the following:
foreach ( 0200 .. 0377 ) { printf FILE "%d , %o , %x: " , $_ , $_ , $_; printf FILE "\cQ%o\n" , $_; }

The hope was that the second printf statement would effectively duplicate the emacs command, but alas...

Hopefully, I've overlooked something particularly simple. Appreciate any ideas you might have... and clarification of the problem might be possible if needed ;)

Replies are listed 'Best First'.
Re: Printing octal characters
by btrott (Parson) on Apr 25, 2000 at 22:31 UTC
    I don't know exactly what you want to appear in FILE, but perhaps something like this would work for you?
    foreach ( 0200 .. 0377 ) { printf "%d , %o , %x: ", $_, $_, $_; print chr $_, "\n"; }
    Is that what you meant?
RE: Printing octal characters
by Anonymous Monk on Apr 25, 2000 at 22:32 UTC
    Unless I've misunderstood, you want to use pack "C", $num to do this.
Re: Printing octal characters
by markguy (Scribe) on Apr 25, 2000 at 22:50 UTC
    Appreciate the help...

    The suggestions from the Anonymous Monk and btrott both do the trick. perlmonkey just went one step too far... the oct() drops it into decimal first.

    Completely forgot that chr() doesn't require decimals and the pack function doesn't see much daylight in my toolbox.

    Thanks again guys!
Re: Printing octal characters
by perlmonkey (Hermit) on Apr 25, 2000 at 22:36 UTC
    Well the oct($str) function will take an octal string $str into decimal and return the value; Then you can print chr(oct($str))." ".$str so print the charcter that corresponds with the decimal value of the octal code. Sort of non intuitive though. Try this:
    foreach ( 0200.. 0377) { print chr(oct($_))." ".$_."\n"; }
    (WARNING: this will give some bogus results and or fail becasuse I was asleep when I wrote it ... see the replies)
      No, that doesn't work. The octal numbers in the foreach loop are converted into decimal internally, so within the loop $_ is actually in decimal, not octal. So you don't need the oct function. In fact, putting in the oct function is wrong, and it will give you errors ("Illegal octal digit ignored ...").

      You can tell by looking at what $_ prints out in your print statement. So you really just need

      print chr $_;
        Doh, you are quite correct. Sorry.

        To print out every possble character this should do it:
        foreach (0 .. 255) { print chr($_)." $_\n"; }
        So to print out the oct 277 you can do "print chr(oct(277))"

        I think I am correct this time.