user-pic

drclaw

Subscribe to feed Recent Actions from drclaw

  • E. Choroba commented on The ordering operators

    I also use it sometimes when there are three different actions based on a comparison, turning something like

    for my $x (1, 2, 3) {
        for my $y (1, 2, 3) {
            print "$x $y ";
            if ($x == $y) {
                print 'same';
            } elsif ($x < $y) {
                print 'less';
            } else {
                print 'greater';
            }
            print "\n";
        }
    }
    

    into

    for my $x (1, 2, 3) {
        for my $y (1, 2, 3) {
            print "$x $y ", qw( same greater less )[$x <=> $y], "\n";
        }
    }
    
  • Aristotle commented on The ordering operators

    Mpapec:

    Unfortunately the code has no control over the format of $version because that value comes from an API. So using a single comparison with padded numbers would require extra logic to munge both values to pad them first. And I can’t think of any even remotely concise and simple way of doing that robustly.

    Can you?

    The best I can come up with is along the lines of

    my @curr_ver = split /[.]/, $version;
    my @len = map length, @curr_ver;
    my ( $curr_padded, $min_padded ) = map…

  • Aristotle commented on The ordering operators

    Question is why at that point you wouldn’t just write this:

    sub vcmp0 {
      my ($v1, $v2) = @_;
      my @v1 = split /[.]/, $v1;
      my @v2 = split /[.]/, $v2;
      $v1[0] <=> $v2[0] || $v1[1] <=> $v2[1] || $v1[2] <=> $v2[2];
    }

    Sure it’s a bit copy-pasty but IMO that’s a benefit rather than a drawback in this case.

Subscribe to feed Responses to Comments from drclaw

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.