peace
camomiletea
Constance - Make tea, not war
Back Navigation Forward
Constance - Make tea, not war: dear diary

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;
 }


Comments
default

Do you have access to modify $.tags? If so, why not sort the array first and then you can just iterate through it when you're done?


default

For some reason, the code is killing my friends page. Would you mind putting it behind a cut, please? :)


default

Sorry! I meant to lj-cut it, I guess I copied wrong piece of code and didn't realize...


default

Thank you! <3


default

Instead of actual removing from the array, you could use a parallel array, which just marks the deleted items. Something like var int[] tagdeleted = 1 .. size($.tags);

Initially set all elements in tagdeleted[] to 0, and then by each "deleting" tags set the matching tagdeleted to 1. And, of course, you will also need appropriately extend the other code to respect tagdeleted.


default

Thank you, that would work.


Back Navigation Forward

Add a little sweetness to your day
There is greatness in smallness.

April 2016
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  

Log In/Out
Username:
Password: