Skip to content

Instantly share code, notes, and snippets.

@mshock
mshock / irnore_chars.pl
Created May 30, 2012 14:02
recursive string snip
#!perl
my $str = '.,a..A,,C..+4ACGTG.,-2TG,,...,a.';
print replace($str);
sub replace {
my $str = shift;
if ($str =~ m/([+-])(\d+)/) {
$str =~ s/\Q$1\E$2.{$2}//;
return replace($str);
}
return $str;
@mshock
mshock / toggle_comments.pl
Created June 7, 2012 19:12
toggle commented script tags in .aspx
use strict;
my $delim = $/;
undef $/;
my $file = <DATA>;
$/ = $delim;
$file =~ s/(<%--)?\s*<script(.*?)>/($1 ? '' : "\n<%-- ") . "<script$2>"/ige;
$file =~ s=</script>\s*(-->)?='</script>' . ($1 ? '' : " -->\n")=ige;
print $file;
__DATA__
<script type="java/js">
@mshock
mshock / data.pl
Created June 7, 2012 20:06
test __DATA__
#! perl
print <DATA>;
__DATA__
<some> <markup>
$this = 'that'
This is me
testing how gist
marks up data token
@mshock
mshock / b64_encode.pl
Created June 11, 2012 17:09
base64 (RFC 2045) encode a file
#! perl
# encode a string/file in Base64 in pure Perl
# edit: cleaner (but non-buffered) version @ http://cpansearch.perl.org/src/GAAS/MIME-Base64-Perl-1.00/lib/MIME/Base64/Perl.pm
use strict;
use Getopt::Std;
use File::Temp qw/tempfile/;
# get and process command line options
my %opts;
@mshock
mshock / grid.pl
Created June 22, 2012 21:04
golf - greatest row || column sum
# current solution using map and autosplit - 80
#!perl -pa
map{$c[$i++]+=$_;$r[$j]+=$_}@F;$i=0;$j++}{$_=(sort{$a<=>$b}@r,@c)[-1]
#########################################
=pod
old solutions and scratch
# run switch solution - 102
#!perl -an
@mshock
mshock / range.pl
Created June 27, 2012 19:07
golf - number range simplification
# current solution - regex - 85
#!perl -p
1while(s/((\d+)-)?(\d+)\.? (\d+)/$3==$4-1?"@{[$2||$3]}-$4.":"$1$3, $4."/e)
######################################################################################
=pod
# scratch and previous solutions
while(s/(\d+)?-?(\d+),? (\d+) \?/$2==$3-1?"@{[$1||$2]}-$3, ":"$2, $3, "/e){}
@mshock
mshock / import.pl
Created June 28, 2012 15:34
import changedb
#!perl
# import data from master to changedb
# changedb implements columns for filenumber, filedate and updateflag
use strict;
use Getopt::Long;
use DBI;
my $dbh;
@mshock
mshock / delete_row.sql
Created July 13, 2012 16:48
delete row-stored columns by row_id
-- delete an entire row_id
delete from rows_of_cols
where row_id in (
select row_id from rows_of_cols
where [filter condition])
@mshock
mshock / reverse.pl
Created July 20, 2012 22:15
golf - reverse numbers game
# current solution using local minima/maxima algorithm - 222
@z=split' ',<>;while(!$t){$t==$_-1?$t=$_:($t=0,last)for@z=(f(shift@z),@z)}sub f{$i++;my($p,$c)=(pop,shift@z);if(($p<$c&&$c>$z[0])||($p>$c&&$c<$z[0])||$#z==-1){if($i+1!=$l){$l=$i+1;print"$l ";$i=0;return($c,$p)}}(f($c),$p)}
=pod
@z=qw(30 13 19 23 18 11 21 8 10 4 29 14 31 27 1 9 22 32 7 2 25 12 17 33 5 26 16 6 35 3 24 28 34 15 20);
while (!$t) {
$t==$_-1?$t=$_:($t=0,last) for @z=( func(shift@z), @z);
#map {$t==$_-1?$t++:0} @z=( func(shift@z), @z);
@mshock
mshock / queens.pl
Created August 7, 2012 22:02
golf - dancing queens
# current solution using regex - 90
#!perl -p0
for$i(0,7..9){for$j(1..7){$c+=()=/(?=(?:_.{$i}){$j}Q|Q(?:.{$i}_){$j})/sg}}$_=$c
=pod
$/=0;$z=<>;
while($i++<7){
$c+=()=$z=~/(?=$_)/sg for(
('_'.'.'x7)x$i.'Q',
('_'.'.'x9)x$i.'Q',