FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Web Development
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
August 12, 2001

TPJ One Liners

(Page 3 of 6)
TPJ One-Liners - The Perl Journal
#21

Picking random elements from an array:

srand;
$item = $array[rand @array];

appeared in Issue 12


#22

If you're trying to get Windows to generate a PostScript file, but it wraps the file with PCL junk, you can remove it with this:

perl -ni -e "!$g&&s/^.*(%!.*)/$1/&&$g or print;last if /^%%
EOF/"

appeared in Issue 12


#23

In the movie Sphere, the commands that Harry typed to translate the message were taken from Tom Christiansen's FAQ:

$BSD = -f '/vmunix'; if ($BSD) {system "BIN
cbreak </dev/tty >/dev/tty 2>&1
sub set_break { # &setset_cbreak(1) or &set_cbreak(0)
                        local($on) = $_[0];
			      local($sgttyb,@ary);
			      require 'sys/ioctl.ph';

Courtesy of Brendan O'Dea
appeared in Issue 12


#24

Seperate the header and body of a mail message into strings

while (<>)  {
    $in_header =   1  ../^$/;
        $in_body   = /^$/ ..eof();

Courtesy of the Perl Cookbook
appeared in Issue 12


#25

Simple numeric tests:

warn "has nondigits" if /\D/;
warn "not a natural number" unless /^\d+$/;
warn "not an integer" unless /^'?\d+$/;
warn "not an integer" unless /^[+']?\d+$/;
warn "not a decimal number" unless
             /^'?\d+\.?\d*$/; # rejects .2
warn "not a decimal number" unless
             /^'?(?:\d+(?:\.\d*)?|\.\d+)$/;
warn "not a C float" unless
             /^([+']?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;

Courtesy of The Perl Cookbook
appeared in Issue 12


#26

Launching xterms with random colors. You might have to replace xterm with whatever command you use to launch a terminal window:

perl -e '$fg=rand 2**24; do { $bg = rand
2**24 } while (unpack("%32b*", pack "N",
($bg^$fg)&0xe0e0e0) < 8); ($fg, $bg) =
map { sprintf "#%06x", $_ } $fg, $bg;
exec("xterm", "-fg", $fg, "-bg", $bg);'

Courtesy of Tkil
appeared in Issue 12


#27

Lop off the latter half of an array:

$#array /= 2;

Courtesy of The Perl Cookbook
appeared in Issue 12


#28

perl -0nal012e '@a{@F}++; print for sort keys %a'
Extracts, sorts, and prints the words from a file.

Courtesy of Peter J. Kernan
appeared in Issue 13


#29

This subroutine accepts a string and returns a true value if all of the parentheses, brackets, and braces in the string are balanced.

sub is_balanced {
                         my $it = $_[0];
                         $it =~ tr/()[]{}//cd;
                         while ($it =~ s/\(\)|\[\]|\{\}//g) { 1 }
                         return !length($it);
                      }

Courtesy Sean M. Burke
appeared in Issue 13


#30

"Regular expressions are to strings what math is to numbers."

--Andrew Clinick, discussing what Microsoft thinks of Perl

in http://msdn.microsoft.com/library/en-us/dnclinic/html/scripting012299.asp.

Short answer: They like it.

Anonymous
appeared in Issue 13




Previous Page | 1 | 2 | 3 | 4 | 5 | 6 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK