Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

TPJ One Liners


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





Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.