Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Programmatically building named anchors to warp to sections

by mreece (Friar)
on Aug 15, 2007 at 01:22 UTC ( #632639=note: print w/replies, xml ) Need Help??


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.

Replies are listed 'Best First'.
Re^2: Programmatically building named anchors to warp to sections
by ww (Archbishop) on Aug 15, 2007 at 12:19 UTC

    As mr_mischief pointed out, your compare starts a new section on EVERY change of the second letter...which is just a frog-hair off plumb from hacker's schema:

    For example, this could make the number of <a name="... /a><c>s and <c><a href="... /a>s very large for a list like:
    Promote
    Psychology
    Ptui
    Pummel

    mreece also makes important points. Please consider them.

Re^2: Programmatically building named anchors to warp to sections
by mr_mischief (Monsignor) on Aug 15, 2007 at 02:07 UTC
    Your code makes a new section for every change in the second letter if the name. hacker wanted just to have sections at the vowels ( first letter . [aeiou] ).

    Also, I'm not sure what's wrong with printing as the output comes, either, instead of building up an array.

    Update: added code tags around character class.
      if hacker really only wants the breaks only on vowels, it is an easy change. (if ($prefix =~ /[aeiou]$/ and $prefix ne $section) { ...?). but it begs the question, what is the first anchor? no anchors at all until you encounter a vowel in the second position? i was hoping to provide a hint to the right direction (substr), without providing a complete solution, because there are a lot of unknowns here!

      the only issue with buffering versus printing is if you want to print the navigational anchor links before the content, without assuming that every anchor will actually exist. it may be best to first determine which anchors will actually be present, then print the navigational links, then the content.

        Why wouldn't an anchor be present for every section? What does it hurt to have a "Bo" header if your first entry in the section starts with "Br"? It still effectively breaks the sections down

        Perhaps the vowels are not the best place to break the entries when one considers the actual frequency of words with [uvwxyz] as the second letter compared to those with [abcd], [efgh], [ijklmn], or [opqrst] as the second letter. There's nothing particularly wrong with those choices, though.

        Having uniform splitting of sections for each starting letter helps navigation quite a bit regardless of where the breaks are made or whether an entry actually starts with what the section header contains.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://632639]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2021-01-26 23:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?