Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Thursday, May 27, 2010

m// in loop

If you loop over m//g, you get one iteration for each match.

$daleks = "Red Dalek Black Dalek Gold Dalek";
while ($daleks =~ /(\w+)\s+Dalek\b/g) {
if (++$count == 3) {
print "The third Dalek is a $1 Dalek.\n";
}
}

NB that $1 always refers to the current match here.

Apparently you shouldn't use 'last' to get out of a loop like this. Don't know why yet.

SQL window functions again

You can think of

window_function() over (partition by A,B,C [order by C,D,E])

as

'Within each group of records with a common A+B+C, return window_function [as if the rows were ordered by C,D,E] but don't group or sort the actual result rows in those ways'

I always mistakenly start by thinking that there is one window function result per A+B+C combination. There is not, there is one result per original query row.

In particular, the order by C,D,E here is an ordering of the original query rows within each group.

Wednesday, May 26, 2010

SQL window functions

Maybe these are easier to understand if you consider the degenerate case where no partition is specified - then the whole table is one partition:

select seqnum, count(*) over () from doctor

gives you count(*) repeated on each row, a bit like Sybase used to do if you missed out the group by.

select seqnum, row_number() over (order by seqnum) from doctor

gives you the sort order number for each row when sorted by seqnum

Similarly, window aggregation functions work without an order by:

select seqnum, count(*) over (partition by doctor_seqnum) from episode

gives (repeated for each episode for a particular Doctor) the number of episodes featuring that Doctor. (You couldn't do row_number() here because it requires a sort).

Thursday, May 13, 2010

POP3 (via C# .NET)

POP3 server responses end in "\r\n". Remember that "\n" in C# is just an 0A like in C, not an 0D 0A like in Perl. Check for this sequence at the end of the byte array you read from the SslStream - don't use the 'wait for a count of 0 bytes to be returned' method in a loop, as you will get a never-ending network wait with that.

Sending TOP m n elicits 3 server responses:
1. +OK
2. The headers, blank line and top n lines, all together
3. A full stop

Followers

Blog Archive