Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • See Sort::Naturally [cpan.org].

    Your idea to use

    split /(\d+)/

    was IMHO an inspired one, so I cannot improve on that idea. Do note that the numeric string sections will always be at the list items with odd index (1, 3, 5, ...) and the string parts at even index (0, 2, 4, ...).

    Here's an alternative implementation, IMO somewhat streamlined, and using an OR-cache (AKA an "Orcish Manoeuvre") to store the split array:

    my %orcache;
    sub macsort {
       my $A = $orcache{$a} ||= [ split /(\d+)/, $a ];
       my $B =

    • Nice implementation. I think the last line is better written as:

      return @$A <=> @$B;
      but since one of them must be zero, the results are equivalent.

      Is i & 1 preferable to i % 1? I consider the latter to be slightly more intuitive.

      BTW you forgot to chomp the data

      Yeah, I skipped that just for simplicity of the example It doesn't affect the test case.
      • I think the last line is better written as:

        return @$A <=> @$B;

        Uh, of course, that's what I planned to write. I had been messing with the implementation showing debugging information during the comparison, and this must be a leftover. That's my story and I'm sticking to it. ;-)

        Is i & 1 preferable to i % 1? I consider the latter to be slightly more intuitive.

        At least it gives the correct results. :-) Surely you ment to type $i % 2.

        A recent thread on Perlmonks [perlmonks.org] by diotalevi shows that nowadays, these instructions take the same time. It used to make a difference, in my favour, 20 years ago before the numerical coprocessors, that all CPUs have

        • Is i & 1 preferable to i % 1? I consider the latter to be slightly more intuitive.
          At least it gives the correct results. :-) Surely you ment to type $i % 2.
          Uh, of course, that's what I planned to write. That's my story and I'm sticking to it. :-P