in reply to Lotto checker...

G'day fishy,

Applying this advice from Arunbear and bigj:

my %draw = map { $_ => 1 } qw( ... ); my @bets = ( ... );

You can reduce your original code, which is somewhat difficult to read and maintain, to just this:

print for map { my $hits = grep { $draw{$_} } @$_; "@$_ --> $hits" . ($hits > 2 && ' *') } @bets;

My test code and results are in the spoiler:

#!/usr/bin/env perl -l use strict; use warnings; my %draw = map { $_ => 1 } qw( 13 15 18 22 41 48 ); my @bets = ( [ qw( 09 13 18 33 39 42 ) ], [ qw( 02 18 32 36 39 42 ) ], [ qw( 05 12 21 22 28 42 ) ], [ qw( 03 14 18 33 34 36 ) ], [ qw( 15 22 26 41 38 48 ) ], [ qw( 06 14 24 26 42 48 ) ], [ qw( 10 28 30 35 36 43 ) ], [ qw( 03 05 08 14 16 35 ) ], [ qw( 08 18 21 35 46 47 ) ], [ qw( 03 15 22 25 46 48 ) ], [ qw( 01 11 38 44 45 49 ) ], [ qw( 14 23 38 39 48 49 ) ], [ qw( 07 17 21 32 45 48 ) ], [ qw( 07 36 37 39 40 41 ) ], [ qw( 07 12 20 29 36 43 ) ] ); print for map { my $hits = grep { $draw{$_} } @$_; "@$_ --> $hits" . ($hits > 2 && ' *') } @bets;

Output:

09 13 18 33 39 42 --> 2 02 18 32 36 39 42 --> 1 05 12 21 22 28 42 --> 1 03 14 18 33 34 36 --> 1 15 22 26 41 38 48 --> 4 * 06 14 24 26 42 48 --> 1 10 28 30 35 36 43 --> 0 03 05 08 14 16 35 --> 0 08 18 21 35 46 47 --> 1 03 15 22 25 46 48 --> 3 * 01 11 38 44 45 49 --> 0 14 23 38 39 48 49 --> 1 07 17 21 32 45 48 --> 1 07 36 37 39 40 41 --> 1 07 12 20 29 36 43 --> 0

-- Ken

Replies are listed 'Best First'.
Re^2: Lotto checker...
by fishy (Friar) on Apr 16, 2014 at 14:22 UTC
    Venerable answers.
    Flexing my mind and stretching my Perl.

    Many Thanks, Monks!

    P.D.
    kcott: just concat "\n" at print argument
      "kcott: just concat "\n" at print argument"

      No. There's no need to do that.

      Look at the full code and output I posted in the spoiler. Note that the output doesn't require additional newlines. Did you run it? Did you get a different result?

      See the first line of that code:

      #!/usr/bin/env perl -l

      Look at "perlrun: Command Switches" to see what the '-l' switch does.

      -- Ken

        Right, Ken.

        I got confused because I ran it with

        /usr/bin/env perl -l

        and got

        /usr/bin/env: perl -l: No such file or directory

        Now, running it with just

        /usr/bin/perl -l

        I get the right result.

        So, no need to append "\n" and another new thing I've learned.


        Thanks a lot!