Constance - Make tea, not war (camomiletea) wrote,
Constance - Make tea, not war
camomiletea

S2 and programming

I had an idea. I wanted to sort tags on the Tags page by the number of time they appear in the journal. I felt it would be useful on a couple of my other journals. But I hit a snag. I don't know enough programming. I figured how I could find the top tag which appears most often, and then I would like to remove this tag from an array and do it again, and again, etc. until there are no more tags. But I don't know if it's possible to remove a particular item from an array and how to do so. And I wanted to try and figure out this on my own, but I should know when I'm out of my depth...

# Sorting tags by $t.use_count

function TagsPage::print_body () {
  # Find leading tag

  var string list;
  var int max = 0;
  var TagDetail leader;

  foreach var TagDetail t ($.tags) {
    if ($t.use_count > $max) {
      $leader = $t;
      $max = $t.use_count;
    }
  }
  var string uses = get_plural_phrase($leader.use_count, "text_tag_uses");
  $list = """<li><a href="$leader.url">$leader</a> - $leader.use_count</li>""";

  # We have a winner. Now repeat it as many times as the number of tags.

  var int[] cycle = 1 .. size($.tags);

    foreach var int top ($cycle) {
      if ($count == size($cycle)) {
        # nil. do nuthin'.
      } else {

      var int max = 0;
      # remove $leader from $.tags array if possible?

      foreach var TagDetail t ($.tags) {
        if ($t.use_count > $max) {
          $leader = $t;
          $max = $t.use_count;
        }
      }

      $list = $list + """<li><a href="$leader.url">$leader</a> - $leader.use_count</li>""";
      $count++;
    }
  }

  # That's it; the party's over.

  print $list;
 }
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 6 comments