Category Archives: coding

how to search a hash-key using regular-expressions

0
Filed under amusing, coding, general, linux, perl

hi,

these days I was encountering a problem, when I was going through 2 veeeeeeery big hashes to find out which of them have similar keys (not really same keys, just slightly different). I thought, that the easiest way would be to go through both hashes in 2 for-loops and check them out one by one. Unfortunately, this approach took about 2 minutes to go through (2 minutes seem like an eternity if you are staring at a terminal).

My first approach looked like this:

for my $a (keys %b) {
    for my $c (keys %d) {
        if ($c =~ m/^$a.+$/) {
            print "bingo!\n";
        }
    }
}

You see, the keys in the second hash (%d) always look like the keys in the first hash (%b) with the only difference, that they contain the key plus some other stuff. So if $a looks like “abc”, $c looks like “abc123″. Now, if you have 2 hashes with – let’s say – 5000 keys and values, you can easily calculate how long this could take…. (5000 x 5000 = 25.000.000) … as I already said: it takes FOREVER!

Now, after some searching around I found a very nice solution on the Linux and Unix Forums from a guy called “pludi”. He recommended a grep-approach. Here is now my remodelled version:

for my $a (keys %b) {
    for my $c (grep { /^$a.+$/ } keys %d) {
        print "bingo!\n";
    }
}

Voila. Instead of taking 2 minutes, the loop now runs through in 15 seconds. :) I love Perl :D

android + ASE + Perl = <3

0
Filed under amusing, android, coding, general, linux, perl, ruby

just started playing with my new motorola milestone with android 2.1. :D
I had no idea how much fun it is to write perl-programs for this nice little device. I use the ASE (android scripting environment) which also supports python, lua, ruby …

You can get it on their website.

I guess I will write some programs soon to see what’s possible. The only thing that sucks on the milestone is, that the dollar sign is only available via touchpad. That’s REALLY annoying. :/

oebbquery – a small train-connection-retrieval-tool :P

2
Filed under amusing, coding, general, linux, perl

ok, today I finished my work at the fancy little script I mentioned yesterday. You can download it now here. Because wordpress for some reason doesn’t allow me to upload files without a filename-extension (grrr) it now has a txt-ending. please just ignore the extension …..

you call the script like this:

$ ./oebbquery "Innsbruck Hbf" "Wörgl Hbf"
Connections from Innsbruck Hbf to Wörgl Hbf:
 
Departure  Arrival  Duration  Changes  Train(s)
15:13      15:59     0:46     0        rex 
15:28      16:24     0:56     0        s 
15:54      16:28     0:34     0        oec

Please be sure to use the correct names of the stations. for example: “wörgl” doesn’t work, but “Wörgl Hbf” works. You have to use the names provided by the oebb-website.

the output looks now like this:
blubb

have fun!

how to get train departure information for your console :P

3
Filed under amusing, coding, general, linux, perl

hi guys,

I just spent 2 hours on a little project here. With this little script written in Perl by using LWP you now have the possibility to view train-connections from A to B in your terminal. You can then use this output to process it with conky or just any application you want. Here is a screenshot:

trains

As you can see, i used it to display it with conky by executing it once every 600 seconds.

As soon as I have cleaned up the code to make it easier to read, you can download it here.

Please … use Here-Documents …

6
Filed under coding, general, linux, perl, ruby

I can’t tell you how often I see scripts and programs which contain a huge amount of echo, print or whatever calls to produce output. It is ok if there are one or two lines to print out, but it becomes inefficient when used explicitly. In shell-scripts and Perl for example you could use so-called Here-Documents. Never heard of them? Well, here some examples:

example: bash

#!/bin/bash
# usage text
usage() {
    echo ""
    echo "This is the usage of blabla"
    echo "Parameters:"
    echo " -f ... Parameter 1 blabla"
    echo " -g ... Parameter 2 blablubb"
    echo ""
}

Well, here you can see that there are a lot of echo-calls (often there are MUCH more … :-/). Now, we learn about an exciting new way to shorten this. We use “cat”:

example: bash better

#!/bin/bash
# usage text
usage() {
    cat << EOUSAGE
 
This is the usage of blabla
Parameters:
 -f ... Parameter 1 blabla
 -g ... Parameter 2 blablubb
 
EOUSAGE
}

So, instead of using tons of calls, just do it with one “cat”. Maybe it seems a bit pedantic of me, but this example makes more sense if you encounter more text (like HTML-output) which is printed out to a document with thousands of “echo”-s.

You can do this in Perl similarly:

#!/usr/bin/perl
# usage text
sub usage() {
    print <<"EOUSAGE";
 
This is the usage of blabla
Parameters:
 -f ... Parameter 1 blabla
 -g ... Parameter 2 blablubb
 
EOUSAGE
}

or in Ruby:

#!/usr/bin/ruby
# usage text
def usage
    print <<EOUSAGE
 
    This is the usage of blabla
    Parameters:
     -f ... Parameter 1 blabla
     -g ... Parameter 2 blablubb
 
EOUSAGE
end

I guess you can do something like that or similarly in nearly every language. So PLEASE, if you have much text to display in your scripts, use Here-Documents.

some information about the eee-PC on current kernel

0
Filed under coding, general, linux

hi,

just wanted to give you a small update about how things are going with the current kernel (2.6.31.1 so far) on the 901 model from Asus.

Graphics:
The intel chipset ( Intel Corporation Mobile 945GME Express Integrated Graphics Controller ) works very well and even KMS works like expected. There were some minor issues that required to build a initial ramdisk in the past, but now that’s not necessary anymore. I don’t know if that’s because of a bugfix or just some lucky configuration of my kernel. ;)

Wireless:
WiFi works with the rt2860 module from /staging.

Bluetooth:
I managed to use my cell phone as a modem device. It wasn’t very difficult. I’ll write a small article soon how to get this done. wvdial and rfcomm do the dirty work anyway …

I also bought a very nice little IBM thinkpad some days ago which I am currently tweaking to fit my needs. Screenshots will follow. Here is one from my eeePC:

endorsing calcurse :)

2
Filed under amusing, artistic, coding, general, linux

Today I found a very nice ncurses-based calendar tool named calcurse. At first it is a bit hard to get used to it, but once you figured out how it works, you’ll love it.
Friends of console applications will love it anyway ;) . I have always been searching for a reasonable console tool for this task. Now I found one :)

Here is the link to the website:
calcurse

I’m using it as my primary calendar application to get an overview of my daily tasks and events.

Here a screenshot of me entering my university lessons :P :

entering stuff in calcurse

entering stuff in calcurse

playing with github – everyone is invited!

0
Filed under biology, coding, general, perl

#### UPDATE #####
To have a look at the project:
click me
###############

hi guys,

I decided to register an account on github to share some of my coding projects with the world. By now I only uploaded huntersim so far but some others will follow soon. If you want to help me by commenting or patching it, feel free to do so. Get an account at github.com and tell me you account name by mail. I could then add you to the collaborators and you can submit your changes directly. If you just want to take a look or run the program you can of course just clone the repository without registration.

The public clone URL is: git://github.com/ap0calypse/huntersim.git

You can clone the repository with the following command:

git clone git://github.com/ap0calypse/huntersim.git

So, if you want to contribute to the project (or fork it, whatever) tell me your username and I add you to the collaborators. You should have some git knowledge. But don’t be scared. You can’t destroy anything :P

huntersim 0.1 – my first simulation

7
Filed under amusing, artistic, biology, coding, linux, perl

hi guys,

some days ago I decided to write a little simulation in Perl. Well, … here is the result (huntersim-0.1). The package contains a README where everything should be explained.

Have FUN!

UPDATE:

Because people want to see pics:


[UPDATE] helper script for changing the MAC

3
Filed under coding, general, linux, perl

UPDATE:
##############

I now rewrote the script a little bit to use a private MAC-area because of problems when generating some MAC-addresses. Please use this version if you want to use it …

##############

hi guys,

yesterday I told you how to change your MAC address. Because I’m unbelievably lazy I decided to write a little helper-script to do this automatically. This script generates a random MAC address and brings the interface up with it.

here it is: change-mac

#!/usr/bin/perl
use strict;
 
# name:     change-mac
# author:   ap0calypse (ap0calypse@agitatio.org)
# purpose:  helper script for bringing an interface down
#           and up again with a random MAC-address
# date:     2009-04-15
 
# replace this with your wlan-interface
my $interface = "wlan0";
 
my @val_mac = (0 .. 9, "A" .. "F");
my $rand_mac = "AC:DE:48:";
 
for (1 .. 6) {
    $rand_mac .= $val_mac[rand(@val_mac)];
    if ( ($_ % 2 == 0) && $_ != 6) {
        $rand_mac .= ":";
    }
}
 
system("ifconfig $interface down");
system("ifconfig $interface hw ether $rand_mac");
system("ifconfig $interface up");