[JaxPM] regex weirdness

Steve Lane sml at zfx.com
Thu Jul 12 11:50:08 CDT 2001


On the jacksonville-pm-list; Jax.PM'er Steve Lane <sml at zfx.com> wrote -

j proctor wrote:
> 
> On the jacksonville-pm-list; Jax.PM'er j proctor <jproctor at oit.umass.edu> wrote -
> 
> I want to sort a list based on the number of Zs each element has in it,
> and I don't have a Cookbook handy.
> 
> Code like this doesn't work:
> 
> @out = sort {
>     (scalar @{[$a =~ /Z/g]}) <=> (scalar @{[$b =~ /Z/g]})
> } @in;

do you have a full program that doesn't work for you?
because this one works for me:

p.s. just "@{[$a =~ /Z/g]} <=> @{[$b =~ /Z/g]}" works
as the sort comparison too.

#!/usr/bin/perl

my @lines = <DATA>;

my @sorted = sort { 
  (scalar @{[$a =~ /Z/g]}) <=> (scalar @{[$b =~ /Z/g]}) 
} @lines;

print "SORTED:\n", @sorted;

__DATA__
alksjdlkajZZZ klsjdlajsdZZ
alkjsZZalsjdlaksZZZZZZZZZZZZZZZZz
askjdlkadsZZZaslkdjlasjZZZZaslkdjalksjdZZZZZZZZZ
aklsjdZZaksjdlaksjd
kajsdlkjzZZZZlkasjdasjd
lkajsdljaskdjla
klasjdlajsdlasjdlkajs
klasjdlaZasldjalskjd
akjsdlZZZZZZZZZZZZZZZZZZZZZZZZdalksjdlsa

--
Steve Lane <sml at zfx.com>

Jax.PM Moderator's Note:
This message was posted to the Jacksonville Perl Monger's Group listserv.
The group manager can be reached at -- owner-jacksonville-pm-list at pm.org
to whom send all praises, complaints, or comments...




More information about the Jacksonville-pm mailing list