[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] crazy hoops to do something that seems so simple?



On Sat, Nov 11, 2000 at 04:30:26AM -0500, Scott R. Godin wrote:
> (first off, there has to be an easier way to generate a variable containing
> a-z and z-a, but since I just created these to test with anyway, it's not
> crucial)
> 
> my $forwards = join('', @{['a'..'z']});

This is just like the unnecessaru hash copy in the other thread.  'a'..'z'
is already a list; putting it in an anonymous array and then dereferencing
it is just silly.

my $forwards = join '', 'a'..'z';

> print $forwards, "\n";
> my $backwards = reverse('a'..'z');

You've got a context problem there.  Anyway, $forwards already holds the
string you want, in the opposite order.

my $backwards = reverse $forwards;

> print $backwards;
> 
> perl 5.004 doesn't understand the sort of
>     my $forwards .= for ('a'..'z');
> context (sigh) that you later version users have, and if I use
>     my $forwards = 'a'..'z';
> I get non-numeric errors in 'flip' and 'flop' whatever they are.

In a scalar context .. is the flip-flop operator.  `perldoc perlop` (or
equivalent with Shuck)

> 
> second, it is apparently not obvious to translate tr/a-z/z-a/ since it
> doesn't work that way, but to have to write

Character class ranges only go from lesser to greater characters.  That's
the usual way of implementing them, not just in Perl.


> (my $coercename = $filename) =~ tr/A-Z/a-z/;
> $coercename =~ tr/abcdefghijklmnopqrstuvwxyz/zyxwvutsrqponmlkjihgfedcba/;
> 
> seems *ridiculously* cumbersome.
> 
> pray tell, is there a better way?

my $coercename = lc $filename;
my $backwards = join '', reverse 'a'..'z';
eval "\$coercename =~ tr/a-z/$backwards/";  # tr/// does not interpolate


> It suddenly dawned on me that while I wanted to sort the ratings DESCENDING,
> I also wanted to sort the filenames ASCENDING
> 
> so that as-mapname (7.5) comes before as-thismapsucks (7.5)
> 
> it seemed to me that the easiest way to do this is to reverse the
> alphabetization of the coerced name key for the hash, but trying to do it
> gave me headaches until I just typed out the entire reversed alphabet (ugh)
> awaiting enlightenment,

sort {$a cmp $b} @list;   # ascending
sort {$b cmp $a} @list;   # descending


map { $_->[0] }
  sort { $b->[1] <=> $a->[1] || $a->[2] cmp $b->[2] }
    map { [$_, /^(\S+)\s+\(([\d.]+)\)/ ? ($2, $1) : (0, '') ] }
      @list;


That's a Schwartzian Transform, with two comparators instead of one.  The
ratings, at $_->[1], are compared first in descending order; if those are
the same, the filenames, at $_->[2], are compared in ascending order.


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org