Tabs-to-space function
sub tabs2space($) { my $str = shift; 1 while $str =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e; return $str; }
DZone Snippets > Minimiscience
11397 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
sub tabs2space($) { my $str = shift; 1 while $str =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e; return $str; }
sub basename($) { my $file = shift; $file =~ s!^(?:.*/)?(.+?)(?:\.[^.]*)?$!$1!; return $file; } sub dirname($) {my $file = shift; $file =~ s!/?[^/]*/*$!!; return $file; }
grep killed /usr/games/lib/nethackdir/logfile | awk -F killed '{print"killed"$NF}' | sort | uniq -c | sort -r
<HTML> <HEAD> <TITLE>Unicode Chart</TITLE> <LINK REL="Stylesheet" TYPE="text/css" HREF="styles.css"> <STYLE TYPE="text/css"> TH {text-align: center; } TD {text-align: center; } </STYLE> </HEAD> <BODY> <TABLE ALIGN=CENTER BORDER=1> <TR><TH> </TH><TH>0</TH><TH>1</TH><TH>2</TH><TH>3</TH><TH>4</TH><TH>5</TH><TH>6</TH><TH>7</TH><TH>8</TH><TH>9</TH><TH>A</TH><TH>B</TH><TH>C</TH><TH>D</TH><TH>E</TH><TH>F</TH></TR> <?PHP for ($i=0; $i<256; $i++) { //DON'T try to generate the whole chart printf('<TR><TD>%04X</TD>', $i); for ($j=0; $j<16; $j++) { printf('<TD>&#x%X%X;</TD>', $i, $j); } echo "</TR>\n"; } ?> </TABLE> </BODY> </HTML>
<?PHP $columns = 3; $im = glob("*.{gif,jpg,png}", GLOB_BRACE); $rows = ceil(count($im) / $columns); for ($i = 0; $i < $rows; $i++) { echo "\n<TR>"; for ($j = $columns*$i; isset($im[$j]) && $j - $columns*$i < $columns; $j++) { echo "<TD>$im[$j]<BR><IMG SRC='$im[$j]'></TD>"; } echo "</TR>"; } ?>
int Fibonacci(int n) { int a = 1, b = 1, c, i; for (i=3; i<=n; i++) {c = b; b += a; a = c; } return b; }
int factorial(int x) { int fac = 1; for (int i=2; i<=x; i++) fac *= i; return fac; }
#define nCr(n, r) (factorial(n) / factorial(n-r) / factorial(r)) #define nPr(n, r) (factorial(n) / factorial(n-r))
int modInverse(int a, int n) { int i = n, v = 0, d = 1; while (a>0) { int t = i/a, x = a; a = i % x; i = x; x = d; d = v - t*x; v = x; } v %= n; if (v<0) v = (v+n)%n; return v; }
int gcd(int x, int y) { int a, b; if (x<y) {a = y; b = x; } else if (x>y) {a = x; b = y; } else {return x; } do { int r = a % b; a = b; b = r; } while (b != 0); return a; }
/* Put this line at the top of the file: */ #include <sys/time.h> /* Put this right before the code you want to time: */ struct timeval timer_start, timer_end; gettimeofday(&timer_start, NULL); /* Put this right after the code you want to time: */ gettimeofday(&timer_end, NULL); double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec) / 1000000.0; printf("Time spent: %.6f\n", timer_spent);