The Alphabet

Oct 20
2009

Python

import re
 
_char = re.compile(r'\w')
 
''.join([chr(i) for i in xrange(65,122) if _char.match(chr(i))])
 
# or just:
import string
 
string.ascii_letters
string.ascii_lowercase
string.ascii_uppercase

Ruby

(65..122).to_a.map{ |e| e.chr =~ /\w/ && e.chr }.compact.to_s

Perl

$chars .= chr() =~ /\w/ ? chr() : q{} for 65..122;

I don’t like the mess of Ruby, and Perl.

just another perlmonks’ post

Feb 06
2009

perldoc perlfaq1 | perl -ne ’/((?< =”)(.+hack…(?:OR)?))/&&die$2.$/’ -

Filtering Lists

Dec 01
2008

Ruby

arr = (1..10).to_a
arr.select{ |e| e % 2 != 0 }

Python

arr = range(1,11)
[e for e in arr if e % 2]

Perl

@arr = (1..10);
grep($_ % 2, @arr);

Ultra Tiny Address Book

Oct 29
2008
#!/usr/bin/perl
 
$s = shift;
$"  = "\n";
$/  = $\;
$_  = <data>;
@_ = /(.*?$s.*)/gi;
 
print "@_\n";
 
__DATA__
 
me "some address" my_phone_here
etc...
etc...</data>

downloader

Jun 09
2008
#!/usr/bin/perl
 
use strict;
use warnings;
use WWW::Mechanize;
use LWP::Simple;
use Data::Dumper;
use Time::HiRes qw( usleep tv_interval gettimeofday );
$|++;
my $url  = shift || die "no url given!\n";
my $ext  = shift || 'mp3'; #die "no file extention given!\n";
my $mech = WWW::Mechanize->new(agent=>"Mozilla/5.0");
my $ua   = LWP::UserAgent->new;
my %seen;
 
$mech->get($url);
$ua->timeout(5);
 
my $links = $mech->links;
 
no warnings;
my $c = 0;
for my $link ( @{$links} ) {
        my $url  = $link->url_abs;
        my $res  = $ua->head($url);
        my $http_res   = HTTP::Response->new($res);
        my $abs_name   = $http_res ->{'_rc'}->{'_previous'}->{'_headers'}->{'location'};
        my ($rel_name) = $abs_name =~ /.+\/(.+)$/;
        my $local_name = $rel_name;
 
        $seen{$local_name}++ and next;
 
        if( $rel_name =~ /\Q$ext\E$/i ){
                $rel_name =~ s/\%\d+/ /g;
                $rel_name =~ s/(.{35}).+/$1.../;
                print(pack('A45', "[$rel_name]"), "  is being downloaded ... ");
                my $s_time = q{};
                my $e_time = q{};
                my $flag = [];
 
                for( 1..5 ){
                        local $SIG{ALRM} = sub { die("timeout") };
                        eval {
                           alarm(10);
                           $flag = [head($abs_name)];
                           alarm(0);
                        };
                        next if $@ =~ m|timeout|;
                        last if $@ !~ m|timeout|;
                }
 
                if( $flag->[0] ){
                        $c++;
                        $s_time = [gettimeofday];
                        getstore($abs_name, $c."-".$local_name);
                        $e_time = tv_interval ($s_time, [gettimeofday]);
                }
 
                $e_time =  $e_time ? $e_time . " seconds" : 'less than 1 milisecond';
                print "timeout failure in $e_time seconds :(\n" and next if $@;
                print "done in $e_time\n";
        }
}
 
`ruby -e 'Dir["*"].each{|f| File.rename f, f.sub(/^\d+-/, "")}'`;
`ruby -e 'Dir["*"].each{|f| File.rename f, f.gsub(/%20/, "_")}'`;

Scalar || List context

Feb 12
2008

Perl

 $a  = ("a", "b", "c"); # $a is "c" - the last element
($a) = ("a", "b", "c"); # $a is "a" - the first element

Ruby

 obj        = ["a", "b", "c"] # obj is Array - ["a", "b", "c"]
 obj, b, c = ["a", "b", "c"] # obj is String - "a"

Perl script opening (vim variant)

Feb 04
2008
#!/bin/bash
 
file=$1
 
if [ -e $file ]
then
        vim $file
else
        touch $file
        echo '#!/usr/bin/perl' > $file
        echo '' >> $file
        echo 'use strict;' >> $file
        echo 'use warnings;' >> $file
        echo 'use diagnostics;' >> $file
        echo '' >> $file
        chmod +x $file
        vim $file
fi

JAPH with AUTOLOAD

Feb 03
2008
#!/usr/bin/perl
 
JAPH->Just->Another->Perl->Hacker;
 
package JAPH;
 
sub AUTOLOAD { bless [print+($AUTOLOAD=~/::(.+)/)[0], q/ /] } sub DESTROY {}

HTML To WIKI converter

Feb 02
2008

Perl script (thanks to the author of HTML::WikiConverter):

#!/usr/bin/perl
 
use strict;
use warnings;
use HTML::WikiConverter;
use Perl6::Slurp;
 
my $file_name = shift;
 
-f $file_name or die "No file given!\n";
 
my $wiki = new HTML::WikiConverter(dialect=>'MediaWiki');
 
print $wiki->html2wiki(slurp $file_name);
 
__END__
 
  Supported dialects:
 
  DokuWiki
  Kwiki
  MediaWiki
  MoinMoin
  Oddmuse
  PbWiki
  PhpWiki
  PmWiki
  SlipSlap
  TikiWiki
  UseMod
  WakkaWiki
  WikkaWiki

File Splitter

Jan 18
2008
use strict;
use Getopt::Std;
use File::Basename;
 
getopts('scf:p:');
 
our($opt_s, $opt_c, $opt_f, $opt_p);
 
my $file = $opt_f;
my $size = $opt_p || 1;
my $len  = 1024;
my $c = 1;
my ($buf, $counter);
 
-f $file or usage();
 
if($opt_s){
    $size = 1024 * $size;
 
    open IN, "< $file" or die "cannot open_r $file $!";
    open OUT, "> $file.$c"  or die "cannot open_w $file $!";
    binmode IN;
    binmode OUT;
 
    while(read(IN, $buf, $len)){
        $counter++;
        if($counter > $size){
            $counter = 0;
            $c++;
            close OUT;
            open OUT, "> $file.$c"  or die "cannot open_w $file $!";
            binmode OUT;
        }
        print OUT $buf
    }
} elsif($opt_c) {
    my @files = grep { -f and /^$file\.\d+$/ } glob '*';
    my $newfile = "splitter_$file";
    open OUT, ">> $newfile" or die "cannot open_w $file $!";
    binmode OUT;
    map { $_ =~ s/^$file\.// } @files;
    for(sort {$a< =>$b} @files ){
        open IN, "$file.$_" or die "cannot open_r $file $!";
        binmode IN;
        print OUT $_ while <in>;
        close IN;
    }
} else {
    usage()
}
 
sub usage{
    my $pro = basename($0);
    print < <SQ;
 
$pro (-s|-c) -p piece size -f filename
 
    -s    split a file into pieces
    -c    collect a file from pieces
    -p     chunk size (defaults to 1MB)
    -f     file to be processed
SQ
    exit 1
}

Calendar

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  

Tags