Skip to content

Instantly share code, notes, and snippets.

@mshock
mshock / 99bottles.pl
Created August 10, 2012 18:52
golf - 99 bottles
# current solution - 187
sub a{$i||99,' bottle',$i!=1&&'s',' of beer',pop||' on the wall'}$i=99;print&a,", ",a('.
'),--$i?"Take one down and pass it around, ":"Go to the store and buy some more, ",&a,".
"while$i
@mshock
mshock / r2d.pl
Created August 20, 2012 17:16
golf - roman to decimal
# current solution - regex - 99
$z=<>;$z=~s/$_(?=([^0-9$_+
-]))?/$1?"-$.+":"$.+"/ge,$.*=$i++%2?2:5for(I,V,X,L,C,D,M);print eval$z.0
=pod
#!perl -p
%h=(I,1,V,5,X,10,L,50,C,100,D,500,M,1e3);
#$m=1;
for$k(keys%h) {
s/$k/$h{$k} /g;
@mshock
mshock / update_report.pl
Created August 20, 2012 20:55
average file update times
#!perl
# compile an average posting time for all files in the 3.x data directory on ftp
# TODO: add latest/earliest columns
use strict;
my $verbose = 0;
my $data_dir = 'D:\Data';
@mshock
mshock / parse_tables.pl
Created August 22, 2012 19:46
parse sql sys text output for tables
# parse sql text sys output to get hashes of table names
sub parse_tables {
my @dbs = @_;
my @db_hashes;
for my $db (@dbs) {
my %db_hash;
for my $line (split "\n", $db) {
if (/^-/../^$/) {
if ($line =~ m/^(\w+)/) {
@mshock
mshock / mtgoc.pl
Created August 22, 2012 19:49
mtgo collection appraisal script
#! perl
# generate pricing report for a csv mtgo collection
# TODO: set completion statistics, TK GUI
use strict;
use LWP::Simple;
use Parse::CSV;
my $buy_flag = 1;
@mshock
mshock / compare_schema.pl
Created August 22, 2012 22:17
compare database tables' existance and schema
#!perl -w
# compare the column schema for all matching tables between two databases
# TODO: auto-fix table/schema mismatch
use strict;
use Config::Simple;
use Getopt::Std;
use POSIX qw(strftime);
use DBI;
@mshock
mshock / table_update.sql
Created August 24, 2012 15:33
find datetime of last sql table update
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID(?)
AND OBJECT_ID=OBJECT_ID(?)
@mshock
mshock / dbh_prep.pl
Created August 24, 2012 16:14
prepare sql query for multiple db handles
# prepare the same sql query string for multiple database handles
sub dbh_prep {
my ($query,@dbhs) = @_;
my @prep_dbhs;
for my $dbh (@dbhs) {
push @prep_dbhs, $dbh->prepare($query);
}
return @prep_dbhs;
@mshock
mshock / init_handles.pl
Created August 24, 2012 19:43
init array of db handles
# create database handles using hashref of db info
sub init_handles {
my @db_info = @_;
my @dbhs;
for my $db (@db_info) {
my $dbh = DBI->connect(
sprintf("dbi:ODBC:Driver={SQL Server};Database=%s;Server=%s;UID=%s;PWD=%s",
$db->{name},
$db->{server},
@mshock
mshock / db_multi.pl
Created August 24, 2012 19:46
dual db sql query execution routine
# perform an operation on the same sql query string for multiple database handles/prepared queries
# db_multi(operation, dbh1, dbh2, optional args)
# operations => optional args:
# prepare => query string (arg2)
# execute => ? paramaters (arg2..)
# finish => none
# fetchall_hashref => key (arg2)
# fetchrow_array => none
sub db_multi {
my ($op, @args) = @_;