A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label count. Show all posts
Showing posts with label count. Show all posts

Faster count(*) in postgres

For an approximate count you can do:

SELECT reltuples FROM pg_class WHERE oid = 'schema_name.table_name'::regclass;

(replacing schema_name and table_name)

(source)

Histogram with perl

perl -lane'($i) = $F[4] =~ m{(\d+)\.}; $m=100; $s=10; for ($c=0; $c<=$m; $c+=$s) { if (($i >= $c) && ($i < $c+$s)) { $h{$c}++; } };END{ for ($c=0; $c<=$m; $c+=$s) { print "$c : ".("#" x $h{$c}); }}'

$i = the value with which you want to make a histogram

$h = hashref holding the histogram data
$s = histogram step size
$m = maximum histogram value

$c = loop counter

Parents node's position in XSL

1. Count parents using <xsl:number>
Number can be extracted:
($n)=$c=~/(\d+)\.\d+\.\d+\.\d+$/;
and adjusted:
$n=((($n/2)-5)+1)

<xsl:number count="." level="multiple" from="/page" format="1"/>

Or with format="a", interpret like j=1, l=2, n=3, p=4, etc.

2. Count parents using position()
Simpler -- but apparently this is a weird way to do it. Probably inefficient too.

<xsl:value-of select="count(parent::*/parent::*/parent::*/preceding-sibling::*) - 3"/>