dd if=/dev/urandom bs=1 count=30 2> /dev/null | \
perl -e 'read(STDIN,$foo,5); print unpack("J",$foo) % 6 . "\n";'
Wed, 23 Jun 2004Yet another random number generator
I was bored...
dd if=/dev/urandom bs=1 count=30 2> /dev/null | \
perl -e 'read(STDIN,$foo,5); print unpack("J",$foo) % 6 . "\n";'
Comments: 0 (view comments)
Regular expression to match quoted and nonquoted strings
The method I was using to match quoted strings didn't work all that well and it wasn't entirely flexible - so I wrote a lamer version which works for everything I've tried -
# Make the underlined part match characters that can't be in a word.
# For HTML, set this to [^\s>]
my $regex = q!(?:"([^"]*)"|'([^']*)'|([^\s]+))!;
while (<>) {
while (s/^$regex\s+//) {
my $string = $1 || $2 || $3;
print "Quoted string: $string
";
}
}
It works, I tested it. Here's some sample output:
nightfall(~) > perl quoteregex.pl testing Quoted string: testing foo bar baz Quoted string: foo Quoted string: bar Quoted string: baz "hello there" how are 'you doing' Quoted string: hello there Quoted string: how Quoted string: are Quoted string: you doing 'foo Quoted string: 'foo 'foo bar baz Quoted string: 'foo Quoted string: bar Quoted string: baz For an example on how to get this to work with html, here's something that'll pull all the links from a webpage (anchor tags, and only the 'href' attribute):
#!/usr/bin/perl
use strict;
use HTTP::Handle;
my $hd = HTTP::Handle->new();
my $regex = q!(?:"([^"]*)"|'([^']*)'|([^\s>]+))!;
$hd->url($ARGV[0] || "http://www.google.com");
$hd->connect();
my $fd = $hd->fd();
undef $/;
my $source = <$fd>;
while ($source =~ s/<a?s+(?:[^>]+s+)*href=$regex[^>]*>//s) {
my $link = $1 || $2 || $3;
print "Link: $link\n";
}
Comments: 0 (view comments)
Sun, 20 Jun 2004Removing duplicates from arrays in Perl
After needing to do this in a project of mine, I went googling and found, as expected, a wide variety of solutions.
Solutions ranged from using map, foreach, grep, etc.. All using things like a temporary hash to count instances and ensure uniqueness - but I remembered that hashes have unique keys and that hashes are often treated the same way as arrays in perl, so my solution is as follows:
my %foo; #Temp var my @a = qw (hello there hello how are you today there what now hello hello hello); %foo = @a; @a = keys(%foo); I also have a one-liner version:
# Assumedly, @a is already defined and has stuff in it, perhaps...
@a = do { my %foo = @a; keys(%foo) };
Comments: 3 (view comments)
Sat, 19 Jun 2004Neat Perl DBI features
Something I'd never bothered doing is reading
perldoc DBI. I was looking through it today looking for a function I forgot the name of and I ran across a function, selectall_hashref. This thing is *totally* sweet. It takes a SQL query and a key field as arguments. It then puts all the results into a hash with the "key field" as the hash key. I never knew about this until now. If you're still at a loss for what it saves you, here's what it shortens:
my $db = DBI->connect(...);
my $res = $db->preprae("SELECT * FROM foo");
$res->execute();
my $foo;
while (my $hr = $res->fetchrow_hashref()) {
$foo->{$hr->{"id"}} = $hr;
}
And using
my $db = DBI->connect(...);
my $foo = $db->selectall_hashref("SELECT * FROM foo", "id");
Comments: 0 (view comments)
Sun, 06 Jun 2004tic 1.0 / Term::Shelly 0.2
Candice wanted a nonsucky, terminal-based aim client, and I was bored enough to
write one. So after about 3 months of off-and-on development I've got enough
work done on it to safely say it's useable. She and I both use it more or less
regularly now. Including work done on my new module, Term::Shelly, I've decided
to tag a release. tic's mostly bug free at the moment and supports a fair set
of the features in AIM.
Features of tic:
You'll need 3 perl modules:
.ticrc syntax
Download tic over there on the right...
Comments: 0 (view comments)
|
Search this siteNavigationMetadataHome About Resume My Code (SVN)ArticlesARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work EfficiencyProjectsfex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scriptsPresentationsYahoo! Hack Day '08 Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials SSH Tunneling (Video)Tag CloudCalendar
FriendsBarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler ShieldsTechnorati |
|||||||||||||||||||||||||||||||||||||||||||||||||