in reply to Pimp my code - Schwartzian transform maybe?

Here's my go at the flagValue sub.

It uses a lookup but also inverts the logic so that an 'R' will return a high number (yours returned 1). Any $flags string that contains an 'R' will be higher than any combination without an 'R' and so on for the others.

You would need to reflect the change in your sort routine.

I would consider processing these values beforehand maybe adding them to @tagIndexes.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %flagvalues = ( R => 2**6, T => 2**5, B => 2**4, I => 2**3, F => 2**2, L => 1, ); my $flags = 'RRTBLXUC'; print flagValue($flags); sub flagValue{ $flags = shift; my $weight; my %unique_flags = map { $_ => undef } split //, $flags; for my $flag (keys %unique_flags){ $weight += exists $flagvalues{$flag} ? $flagvalues{$flag} : 0; # print "$flag -> $weight\n"; } return $weight; }
Update:

It also relies on any flag appearing only once.

Update 2:

Updated to only weigh a flag once.

Replies are listed 'Best First'.
Re^2: Pimp my code - Schwartzian transform maybe?
by GrandFather (Saint) on May 08, 2006 at 12:05 UTC

    Interesting. A little tweaking and we come up with the following which preserves the ordering and priority encoding nature of the original function. Note in particular that the addition is replaced by a bitwise or - that's where the priority encoding bit happens (it is also unaffected by duplicate flags). The bitwise complements preserve the original sort order.

    my %flagvalues = ( R => ~(2**1 - 1), T => ~(2**2 - 1), B => ~(2**3 - 1), I => ~(2**4 - 1), F => ~(2**5 - 1), L => ~(2**6 - 1), ); sub flagValue { my $weight = ~(2**7 - 1); for (split //, shift){ $weight |= $flagvalues{$_} if exists $flagvalues{$_}; } return ~$weight; }

    DWIM is Perl's answer to Gödel