Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Ascii Chart

by runrig (Abbot)
on Feb 25, 2003 at 18:49 UTC ( [id://238527] : note . print w/replies, xml ) Need Help??


in reply to Re: Ascii Chart
in thread Ascii Chart

The data already existed, so when I wrote the program I tried to do as little work as possible to format it. I disagree with the 'apparent need', though, since if there was no need there wouldn't be an ascii man page on the systems that have one (of course, on those systems with an ascii man page, I agree that there is no apparent need for this script).

I thought about using printf to format the numbers rather than leaving them hard coded, but at the time I liked the idea of leaving it hard-coded for easy viewing and re-usability of the data (and it required less work on my part). And using $. is a good idea, but likewise I'd rather be able to look at the data and see which line is which.

If I were to use formats, format and write would probably have to be formline and $^A since the format is dynamic, but that and EBCDIC and locale specific code pages and unicode are left as projects for those with more time than I have :-)

Taking out the hard coding of the hex and binary numbers wasn't too hard though, so here's my first pass at that (I may add octal later)(Update: Added octal - didn't want to do it until I tested it, and zentara is right, it was easy enough):

#!/usr/bin/perl -l # # Display ascii table # (Some unix systems have an 'ascii' man page which displays # an ascii table; this one doesn't, so I cooked this up) # takes optional format string # Usage: ascii [D|a|k|d|b|h|o]... # D=Description a=abbreviation k=Keyboard d=Decimal b=Binary h=Hex o=o +ctal # e.g.: ascii hD # (Default is "ha") # use strict; use warnings; my $display = @ARGV ? join('', @ARGV) : "ha"; $display =~ tr/-//d; my @names = qw(Description Abbr Keybd Dec Binary Hex Oct); my @lengths=qw(-21 -3 -7 3 8 2 3); my %lengths; @lengths{@names} = @lengths; my %show; my @show = qw(D a k d b h o); @show{@show} = @names; my %fmt = (Dec=>"%3d", Binary=>"%08b", Hex=>"%02X", Oct=>"%03o"); my $show_str = join("|", @show); my $usage_str = <<EOT; Usage: ascii [$show_str]... D = description a = abbreviation k = keybd d = decimal b = binary h = hex o = octal (Default: ha) EOT my @display = map { $show{$_} || die "Bad field $_\n$usage_str" } split '', $display; die "No fields to display\n" unless @display; my @ascii; while (<DATA>) { chomp; next if /^#/ or /^\s*$/; my @fields; # Read in pipe delimited file (pipes are escaped) push @fields, $1 while /((?:\\.|[^|])+)/g; s/\\(.)/$1/g for @fields; my %fields; @fields{@names} = @fields; push @ascii, \%fields; } my $max = 70; my $spaces = 3; my $hdr = ' ' x $spaces; my $hdr_txt = sprintf join('', map { "%-" . (abs($lengths{$_})+$spaces) ."s" } @display ), @display; $hdr .= $hdr_txt while length($hdr.$hdr_txt)-$spaces <= $max; print $hdr; my $line = ''; for my $asc (@ascii) { my $tmp_line = ''; for my $fld (@display) { $tmp_line .= " - " if $tmp_line; $tmp_line .= sprintf($fmt{$fld} || "%$lengths{$fld}s", $fmt{$fld} ? $asc->{Dec} : $asc->{$fld}); } if ((length($line) + length($tmp_line) + 3) > $max) { print $line; $line = " $tmp_line"; } else { $line .= " $tmp_line"; } } print $line if $line; __DATA__ # Description|Abbrev|Keybd|Decimal|Binary|Hex Null|NUL|Ctrl @|0 Start of Heading|SOH|Ctrl A|1 Start of Text|STX|Ctrl B|2 End of Text|ETX|Ctrl C|3 End of Transmit|EOT|Ctrl D|4 Enquiry|ENQ|Ctrl E|5 Acknowledge|ACK|Ctrl F|6 Bell|BEL|Ctrl G|7 Back Space|BS|Ctrl H|8 Horizontal Tab|TAB|Ctrl I|9 Line Feed|LF|Ctrl J|10 Vertical Tab|VT|Ctrl K|11 Form Feed|FF|Ctrl L|12 Carriage Return|CR|Ctrl M|13 Shift Out|SO|Ctrl N|14 Shift In|SI|Ctrl O|15 Data Line Escape|DLE|Ctrl P|16 Device Control 1|DC1|Ctrl Q|17 Device Control 2|DC2|Ctrl R|18 Device Control 3|DC3|Ctrl S|19 Device Control 4|DC4|Ctrl T|20 Negative Acknowledge|NAK|Ctrl U|21 Synchronous Idle|SYN|Ctrl V|22 End of Transmit Block|ETB|Ctrl W|23 Cancel|CAN|Ctrl X|24 End of Medium|EM|Ctrl Y|25 Substitute|SUB|Ctrl Z|26 Escape|ESC|Ctrl [|27 File Separator|FS|Ctrl \\|28 Group Separator|GS|Ctrl ]|29 Record Separator|RS|Ctrl ^|30 Unit Separator|US|Ctrl _|31 Space| | |32 Exclamation Point|!|Shift 1|33 Double Quote|"|Shift `|34 Pound/Number Sign|#|Shift 3|35 Dollar Sign|$|Shift 4|36 Percent Sign|%|Shift 5|37 Ampersand|&|Shift 7|38 Single Quote|'|'|39 Left Parenthesis|(|(|40 Right Parenthesis|)|)|41 Asterisk|*|*|42 Plus|+|+|43 Comma|,|,|44 Hyphen / Minus Sign|-|-|45 Period|.|.|46 Forward Slash|/|/|47 Zero Digit|0|0|48 One Digit|1|1|49 Two Digit|2|2|50 Three Digit|3|3|51 Four Digit|4|4|52 Five Digit|5|5|53 Six Digit|6|6|54 Seven Digit|7|7|55 Eight Digit|8|8|56 Nine Digit|9|9|57 Colon|:|Shift ;|58 Semicolon|;|;|59 Less-Than Sign|<|Shift ,|60 Equals Sign|=|=|61 Greater-Than Sign|>|Shift .|62 Question Mark|?|Shift /|63 At Sign|@|Shift 2|64 Capital A|A|Shift A|65 Capital B|B|Shift B|66 Capital C|C|Shift C|67 Capital D|D|Shift D|68 Capital E|E|Shift E|69 Capital F|F|Shift F|70 Capital G|G|Shift G|71 Capital H|H|Shift H|72 Capital I|I|Shift I|73 Capital J|J|Shift J|74 Capital K|K|Shift K|75 Capital L|L|Shift L|76 Capital M|M|Shift M|77 Capital N|N|Shift N|78 Capital O|O|Shift O|79 Capital P|P|Shift P|80 Capital Q|Q|Shift Q|81 Capital R|R|Shift R|82 Capital S|S|Shift S|83 Capital T|T|Shift T|84 Capital U|U|Shift U|85 Capital V|V|Shift V|86 Capital W|W|Shift W|87 Capital X|X|Shift X|88 Capital Y|Y|Shift Y|89 Capital Z|Z|Shift Z|90 Left Bracket|[|[|91 Backward Slash|\\|\\|92 Right Bracket|]|]|93 Caret|^|Shift 6|94 Underscore|_|Shift -|95 Back Quote|`|`|96 Lower-case A|a|A|97 Lower-case B|b|B|98 Lower-case C|c|C|99 Lower-case D|d|D|100 Lower-case E|e|E|101 Lower-case F|f|F|102 Lower-case G|g|G|103 Lower-case H|h|H|104 Lower-case I|I|I|105 Lower-case J|j|J|106 Lower-case K|k|K|107 Lower-case L|l|L|108 Lower-case M|m|M|109 Lower-case N|n|N|110 Lower-case O|o|O|111 Lower-case P|p|P|112 Lower-case Q|q|Q|113 Lower-case R|r|R|114 Lower-case S|s|S|115 Lower-case T|t|T|116 Lower-case U|u|U|117 Lower-case V|v|V|118 Lower-case W|w|W|119 Lower-case X|x|X|120 Lower-case Y|y|Y|121 Lower-case Z|z|Z|122 Left Brace|{|Shift [|123 Vertical Bar|\||Shift \\|124 Right Brace|}|Shift ]|125 Tilde|~|Shift `|126 Delete|Del|Del|127