Archive for July, 2004

Jul
24

crontab Format

Posted by: celso | Comments (0)

[I could never remember this so I'm putting it here.]
These are the fields of a crontab file:


a b c d e /full/path/to/script

where

a = minute (0-59),
b = hour (0-23),
c = day of the month (1-31),
d = month of the year (1-12),
e = day of the week (0-6 with 0=Sunday).

* = every min, hour, day, etc.
*/10 = every 10 min, hour, day, etc.
Categories : Linux, Notes
Comments (0)
Jul
21

Read A Text File

Posted by: celso | Comments (0)

This will open a text file and read it line by line. As it is, it will just print each line to the screen, so very much like the unix ‘cat’ command, but could very well do anything on the line just read by replacing the cout statement.


#include <fstream>
#include <iostream>
#include <string>

void main() {
    string s;
    ifstream infile;

    infile.open("aaa.txt");

    while(infile >> s) {
        cout << s << endl;
    }
}
Categories : Cpp, Notes
Comments (0)
Jul
12

Create Skeleton Module

Posted by: celso | Comments (0)

Use this to create a skeleton module:


h2xs -AXn Your::Module
Categories : Notes, Perl
Comments (0)
Jul
08

stty Settings

Posted by: celso | Comments (0)

This is my stty setting (all in one line):


stty intr '^c' erase '^?' kill '^u' echoe \
echoctl echoke -ixany
Categories : Linux, Notes
Comments (0)
Jul
02

Detect CPU Endian-ness

Posted by: celso | Comments (0)

To detect a CPU’s endian architecture, use either one of the variables set like so:


$is_big_endian = unpack("h*", pack("s", 1)) =~ /01/;
$is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;

Found in Perlmonks

Categories : Notes, Perl
Comments (0)
Jul
02

Sorting Section Numbers

Posted by: celso | Comments (0)

Here’s a code to sort section numbers in ascending order:


sub sort_sections {
    my ($data) = @_;

    my $sorted = ();
    @$sorted = map { $_->[0] }
        sort {
            my $x=1;
            while (defined $b->[1][$x]) {
                defined $a->[1][$x] or return -1;
                if ($x%2) {
                    ## Strict numeric comparison
                    return 1
                        if $a->[1][$x] > $b->[1][$x];
                    return -1
                        if $a->[1][$x] < $b->[1][$x];
                } else {
                    ## Non-numeric comparison
                    return 1
                        if $a->[1][$x] gt $b->[1][$x];
                    return -1
                        if $a->[1][$x] lt $b->[1][$x];
                }
                $x++;
            }
            return defined $a->[1][$x] ? 1 : 0;
        }
        map { [$_, [split(/(\d+)/, $_)]] } @$data;

    return $sorted;
}

Here’s a test for it:


$sects = ['1.1', '1.2.2', '1.3', '1.2', '1.3.1'];
print Dumper($sects);

$sorted_sects = sort_sections($sects);

use Data::Dumper;
print Dumper($sorted_sects);

And here’s the output:


$VAR1 = [
          '1.1',
          '1.2.2',
          '1.3',
          '1.2',
          '1.3.1'
        ];
$VAR1 = [
          '1.1',
          '1.2',
          '1.2.2',
          '1.3',
          '1.3.1'
        ];

Found in Perlmonks.

Categories : Notes, Perl
Comments (0)
Jul
01

Base Conversion

Posted by: celso | Comments (0)

The following routines will convert a number to and from among the different bases: decimal, hexadecimal, and binary.


################################################
# Convert a binary input to hex
# Does not return any leading 0s
#
 sub bin2hex {
    my $inpt = shift;
    my $hex;
    my $bits = length($inpt);
    $inpt = (32 - $bits) x '0' . $inpt;
    my $dec = unpack("N",
                 pack("B32", substr("0" x 32 . $inpt, -32)));

    return(sprintf("%x", $dec));
}

################################################
# Convert a decimal input to binary
# Arguments = decimal_number, number_of_bits
#
sub dec2bin {
    my $dec = int(shift);
    my $bits = shift;
    my $bin = unpack("B32", pack("N", $dec));
    substr($bin, 0, (32 - $bits)) = '';
    return($bin);
}

################################################
# Convert a binary input to decimal
#
sub bin2dec {
    my $bin = shift;
    my $bits = length($bin);
    $bin = (32 - $bits) x '0' . $bin;
    my $dec = unpack("N",
                 pack("B32", substr("0" x 32 . $bin, -32)));
    return($dec);
}

###############################################
# Convert a hex input to decimal
#
sub hex2dec {
    my $h = shift;
    $h =~ s/^0x//g;
    return( hex($h));
}
Categories : Notes, Perl
Comments (0)

Gallery