in reply to Remove lowest number

my @d1s = sort { $b <=> $a } $d61 ; my @d2s = sort { $b <=> $a } $d62 ; my @d3s = sort { $b <=> $a } $d63 ; my @d4s = sort { $b <=> $a } $d64 ;

What is the point of this? Sorting lone scalars, and putting them in one element arrays? What's the point of chomping $roll? I would write that as:

#!/usr/bin/perl use strict; use warnings; my $tosses = 5; my $dice = 4; foreach (1 .. $tosses) { my @rolls = sort {$b <=> $a} map {2 + int rand 5} 1 .. $dice; pop @rolls; my $sum = 0; $sum += $_ for @rolls; print "\n@rolls: $sum\n\n"; } __END__

And that includes finding the top-3 rolls and summing them.

Abigail

Replies are listed 'Best First'.
Re: Re: Remove lowest number
by ellem (Hermit) on Aug 06, 2003 at 17:04 UTC
    First let's understand that I have no idea what I am doing and only a murky idea of what I want to do!

    OK I'll give you the chomp I think that had something to do with an older version of this. See mostly I make Perl read logs and take data from different sources and shove it all together. But I don't do this everyday and as such I suck at it.

    OK, so I thought:
    my @num = (8, 9, 3, 5, 1, 2, 6, 7) ; my @all = sort { $b <=> $a } @num ;
    thus:
    my $num5 = 5 ; my $num4 = 4 ; my $num3 = 3 ; my $num2 = 2 ; my @all = sort { $b <=> $a } $num4, $num2, $num5, $num3 ;
    So when I was writing this code:
    my @d1s = sort { $b <=> $a } $d61 ;
    I thought I was sorting $d61 and I thought it might be hard to get at the lowest number inside $. Apparently I was wrong, but your code is over my head.
    $sum += $_ for @rolls;
    Doesn't mean a lot to me. The whole $_ thing confuses me.

    UPDATE:

    I really looked at this answer, suggestion and I started commenting things out to see which part of my code was really useless. Here's what is left:
    #! /usr/local/bin/perl use diagnostics ; use warnings ; use strict ; use List::Util qw (sum) ; #not in the standard distro my $roll = 6 ; until ($roll == 0) { #rolls the dice 6 times $roll -- ; my @d61 = int(rand(5) + 2) ; #6 sided die with no 1 my @d62 = int(rand(5) + 2) ; my @d63 = int(rand(5) + 2) ; my @d64 = int(rand(5) + 2) ; my @all = sort #split line for looks { $b <=> $a } @d61, @d62, @d63, @d64 ; #sorts rolls #to prepare for pop @all ; #pop removing #the lowest roll my $sum = sum(@all) ; #before adding print "\n@all - $sum\n" ; }

    --
    ellem@optonline.net
    There's more than one way to do it, but only some of them actually work.