Seeing as it's election day here in Canada, this code tells you who you should vote for.
#!/usr/bin/perl -w use strict; my @parties = (Liberal, Conservative (boo, hiss), NDP, Green); my $party = rand[1..4]; print $parties[$party];
I wrote this on a computer without Perl, so it probably doesn't work. Please let me know what I did wrong.


our @item = reverse (114, 101, 107, 99, 97, 104, 32, 108, 114, 101, 80, 32, 114, 101, 104, 116, 111, 110, 97, 32, 116, 115, 117, + 74); local $my = reverse ")meti@\ ,rhc (pam tnirp";eval $my;

Replies are listed 'Best First'.
Re: Vote decider
by gaal (Parson) on Jun 29, 2004 at 10:26 UTC

    perl -MList::Util=shuffle -le 'print ((shuffle(qw(Liberal Conservative NDP Green)))[0])'

    Pros: no hardcoding of list length
    Cons: O(n). But voting is not a lighthearted matter, and is worth spending some time mulling over.

Re: Vote decider
by mawe (Hermit) on Jun 28, 2004 at 16:00 UTC
    Hi!
    ...so it probably doesn't work...
    You're right!
    #!/usr/bin/perl -w use strict; my @parties = qw(Liberal Conservative NDP Green); my $party = rand(4); print $parties[$party];
    This should work.
    mawe

      Avoid writing literals like 4 into your source code and use the length of the array as an argument for itself.

      print $parties[ rand @parties ];

      Or.

      my $party_ix = rand @parties; print $parties[ $party_ix ];
        Good point! Thank you!

        mawe
Re: Vote decider
by saskaqueer (Friar) on Jun 29, 2004 at 06:30 UTC
    # *nix perl -le 'print qw(Liberal Conservative NDP Green)[rand 4]' # win32 perl -le "print qw(Liberal Conservative NDP Green)[rand 4]"
Re: Vote decider
by Anonymous Monk on Jul 08, 2004 at 15:19 UTC
    #!/usr/bin/perl -w use strict; my @parties = qw(Liberal Labour Greens Democrats National One-nation); my $party = rand(6); print "$parties$party\n"; I like my new lines. :-)