http://qs321.pair.com?node_id=632639


in reply to Programmatically building named anchors to warp to sections

assuming your rows are sorted by name, you just need to get the relevant substring(), compare it to the 'current' substring, and print out an anchor when the new substring doesn't match the current one.
my $section = ''; while (my $row = $sth->fetchrow_hashref) { my $prefix = substr($row->{name}, 0, 2); if ($prefix ne $section) { $section = $prefix; print qq{<a href="#$section">$section</a>}; } # do the usual }
instead of printing, you'll probably want to append the content to a variable, and push each $section into a @list, so you can then print the navigation header before printing the output buffer.