in reply to Re: sorting an array with decimal points
in thread sorting an array with decimal points

G'day Laurent,

An alternative to this could be a Guttman-Rosler Transform. In the following, I've kept the same data, in the same order; and, I retained the same basic code layout for the transform.

#!/usr/bin/env perl -l use strict; use warnings; my @array = qw{ Patch_11.4 Patch_1.0 Patch_2.0 Patch_3.1 Patch_5.0 Patch_4.2 Patch_6.0 Patch_11.0 Patch_7.0 Patch_8.0 Patch_9.3 Patch_10.2 Patch_11.2 }; @array = map substr($_, 2), sort map pack("C2", /^Patch_(\d+)\.(\d+)/) . $_, @array; print for @array;

The output is identical to what you show.

Just as a side note, given the list context provided by "[ ... ]", regex captures will be evaluated in that context, and your second map would only need a single statement. Here's a quick one-liner to explain:

$ perl -E 'my @x = qw{X1.2 X3.4}; say "@$_" for map { [ $_, /X(\d+)\.( +\d+)/ ] } @x' X1.2 1 2 X3.4 3 4

— Ken

Replies are listed 'Best First'.
Re^3: sorting an array with decimal points
by Laurent_R (Canon) on Jan 15, 2018 at 07:26 UTC
    Hi Ken,

    Thank you for your comment. In fact, I had briefly thought about the GRT, but did not find a nice implementation of it in that specific case, so I stuck to the more traditional ST.

    You're right about that fact that the initial map in the ST solution can be made in only one statement. Given that the ST is a bit complicated to understand for a beginner not knowing about it, I thought it would be a little clearer with two separate statements in the map, I'm no longer sure that this is really the case.