Archive for July, 2004
crontab Format
Posted by: | Comments[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.
Read A Text File
Posted by: | CommentsThis 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; } }
Create Skeleton Module
Posted by: | CommentsUse this to create a skeleton module:
h2xs -AXn Your::Module
stty Settings
Posted by: | CommentsThis is my stty setting (all in one line):
stty intr '^c' erase '^?' kill '^u' echoe \ echoctl echoke -ixany
Detect CPU Endian-ness
Posted by: | CommentsTo 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
Sorting Section Numbers
Posted by: | CommentsHere’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.
Base Conversion
Posted by: | CommentsThe 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)); }