in reply to [perlre/perlgolf] Golf this: return -1 or return +1 on regexp subroutine please

77.

1 2 3 4 5 6 7 + 1234567890123456789012345678901234567890123456789012345678901234567890 +1234567 sub by_priority{my$r=qr/^(?:Service|Organisation|FAQ)/;($a=~$r&&-1)+($ +b=~$r)}

Update: As anon. below points out, I hadn't read the spec properly, so... for a less desireable 89 85 88. Instead of Sunset strip, thats two fat ladies.

# 1 2 3 4 5 6 +7 8 #234567890123456789012345678901234567890123456789012345678901234567890 +123456789012345678 sub by_priority{my$r=qr/^(?:Service|Organisation|FAQ)/;($a=~$r&&-1)+($ +b=~$r)||$a cmp$b;}

Test code and results (Hopefully I didn't try a step to far this time.)

#! perl -sw use strict; # 1 2 3 4 5 6 +7 8 #234567890123456789012345678901234567890123456789012345678901234567890 +12345678901234567 sub by_priority{my$r=qr/^(?:Service|Organisation|FAQ)/;($a=~$r&&-1)+($ +b=~$r)||$a cmp$b} my @stuff = qw/ the Services_s FAQ_A quick FAQ_C Organisation_3 brown FAQ_B fox Services_p jumps Services_q Over Organisation_1 A lazy Organisation_2 dog /; sub shufl (\@) { my $r=pop; $a = $_ + rand @{$r} - $_ and @$r[$_, $a] = @$r[$a, $_] for (0..$#{$r}); } shufl @stuff; print $_,$/ for sort by_priority @stuff; __END__ c:\test>test FAQ_A FAQ_B FAQ_C Organisation1 Organisation2 Organisation3 Servicesp Servicesq Servicess A Over brown dog fox jumps lazy quick the c:\test>

Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re: Re: [perlre/perlgolf] Golf: Sunset strip!
by Anonymous Monk on Oct 31, 2002 at 23:55 UTC
    Still broken!

    sub by_priority{my$r=qr/^(?:Service|Organisation|FAQ)/;$a cmp$b|($a=~$ +r&-1)+($b=~$r)} my @foo =( "Service", "FAQ", "BrowserUK","Organization1", "FAQ2", "Org +anization2"); print join " ", sort by_priority @foo; __END__ BrowserUK FAQ FAQ2 Organization1 Organization2 Service

    This turns into an alphabetical sort!

      Update: Fixed the data and the code above.

      Okay. All my non-prioritised items started with lower case, so they got sorted last. D'oh!

      Evidence for the defence.


      Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: Re: [perlre/perlgolf] Golf: Sunset strip!
by Anonymous Monk on Oct 31, 2002 at 23:16 UTC
    This will return 0 every time that both $a and $b contain Service|Organiz(s)ation|FAQ1. For example, a list like qw/Organization1 FAQ Organization2 Service Organization3/ will be unaltered by the sort!

    1I guess you really are from the UK, huh? Next you'll be saying there's a 'u' in 'color'!