For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > how do I shorten this code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how do I shorten this code
itshardtogetone@hotmail.com

2008-03-09, 7:16 pm

Hi,
I wish to shuffle 8 decks of cards, so how do I shorten this code :-

use Algorithm::Numerical::Shuffle qw /shuffle/;
@eightdecks = shuffle (1..10,"J","Q","K",1..10,"J","Q","K",1..10,"J","Q","K");#how do I repeat 1 to K 32 times
print @eightdecks ;


Chas. Owens

2008-03-09, 7:16 pm

On Sun, Mar 9, 2008 at 12:23 PM, <itshardtogetone@hotmail.com> wrote:
> Hi,
> I wish to shuffle 8 decks of cards, so how do I shorten this code :-
>
> use Algorithm::Numerical::Shuffle qw /shuffle/;
> @eightdecks = shuffle (1..10,"J","Q","K",1..10,"J","Q","K",1..10,"J","Q","K");#how do I repeat 1 to K 32 times
> print @eightdecks ;



The x operator can repeat a string or list n times:

my @eightdecks = shuffle ((2..10, qw/J Q K A/) x 32);


--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
itshardtogetone@hotmail.com

2008-03-11, 7:05 pm

Hi,
How do I shorten this code:-

if $playercard1 = 10 or $playercard1 = "J" or $playercard1 = "Q" or
$playercard1 = "K";

Thanks

Rodrick Brown

2008-03-11, 7:05 pm

On Tue, Mar 11, 2008 at 2:23 PM, <itshardtogetone@hotmail.com> wrote:
> Hi,
> How do I shorten this code:-
>
> if $playercard1 = 10 or $playercard1 = "J" or $playercard1 = "Q" or
> $playercard1 = "K";
>
> Thanks
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


#!/usr/bin/perl -w
use strict;
use warnings;

my $playercard1=uc <STDIN>;
print "Player has high card\n" if ( $playercard1 =~ /10|J|Q|K/ )


--
Rodrick R. Brown
http://www.rodrickbrown.com
http://www.linkedin.com/in/rodrickbrown
Tom Phoenix

2008-03-11, 7:05 pm

On Tue, Mar 11, 2008 at 11:23 AM, <itshardtogetone@hotmail.com> wrote:

> How do I shorten this code:-
>
> if $playercard1 = 10 or $playercard1 = "J" or $playercard1 = "Q" or
> $playercard1 = "K";


Here's my version of a program that does the same thing as yours. It's
got some advantages over yours, but it's not really shorter:

#!/usr/bin/perl

use strict;
use warnings;

print qq{syntax error at - line 1, near "if \$playercard1 "\n};
print qq{Execution of your_broken_code aborted };
print qq{due to compilation errors.\n};

You can optimize your code for many things; for speed, for code size,
for understandability. But making it work right in the first place is
more important than any of those other things.

ObPerl:

my %tenjqk = map +($_,1), qw{10 J Q K};
print "gotcha\n" if $tenjqk{$playercard1};

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Gunnar Hjalmarsson

2008-03-11, 7:05 pm

itshardtogetone@hotmail.com wrote:
> How do I shorten this code:-
>
> if $playercard1 = 10 or $playercard1 = "J" or $playercard1 = "Q" or
> $playercard1 = "K";


That code does not compile. Besides that it would not do what you think
it would.

You probably want something like:

if ( grep $_ eq $playercard1, qw/10 J Q K/ ) {
# do something
}

Please read about the grep() function in "perldoc -f grep", and about
equality operators and the qw// operator in "perldoc perlop".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Rodrick Brown

2008-03-11, 7:05 pm

On Tue, Mar 11, 2008 at 2:46 PM, Rodrick Brown <rodrick.brown@gmail.com> wrote:
>
> On Tue, Mar 11, 2008 at 2:23 PM, <itshardtogetone@hotmail.com> wrote:
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
>
> my $playercard1=uc <STDIN>;
> print "Player has high card\n" if ( $playercard1 =~ /10|J|Q|K/ )
>


Opps I ment to type the following instead.

print "Player has high card\n" if ( $playercard1 =~ /^(10|J|Q|K)$/ )
John W. Krahn

2008-03-11, 7:05 pm

Rodrick Brown wrote:
> On Tue, Mar 11, 2008 at 2:46 PM, Rodrick Brown <rodrick.brown@gmail.com> wrote:
>
> Opps I ment to type the following instead.
>
> print "Player has high card\n" if ( $playercard1 =~ /^(10|J|Q|K)$/ )


Or even better:

print "Player has high card\n" if $playercard1 =~ /\A(?:10|[JQK])\z/;



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Uri Guttman

2008-03-11, 7:05 pm

>>>>> "JWK" == John W Krahn <krahnj@telus.net> writes:

JWK> Rodrick Brown wrote:[color=darkred]

JWK> Or even better:

JWK> print "Player has high card\n" if $playercard1 =~ /\A(?:10|[JQK])\z/;

a hash lookup is even simpler. i leave that an exercise to the OP.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
itshardtogetone@hotmail.com

2008-03-11, 7:05 pm

Thank You very much everyone here.
Thanks for providing me leads to study.

----- Original Message -----

"John W. Krahn" <krahnj@telus.net>,
Rodrick Brown wrote:
"Tom Phoenix" <
Gunnar Hjalmarsson

itshardtogetone@hotmail.com

2008-03-24, 7:05 pm

Hi ,
Looking at the reg expression ( $playercard1 =~ /10|J|Q|K/ ),
it will match 0, 1, and '10' , J, Q, K

What must I do so that it will match only '10' , 'J', 'Q', and 'K'

Thanks

----- Original Message -----
From: "Rodrick Brown" <rodrick.brown@gmail.com>

> #!/usr/bin/perl -w
> use strict;
> use warnings;
>
> my $playercard1=uc <STDIN>;
> print "Player has high card\n" if ( $playercard1 =~ /10|J|Q|K/ )
>


Yitzle

2008-03-24, 7:05 pm

On Mon, Mar 24, 2008 at 9:00 AM, <itshardtogetone@hotmail.com> wrote:
> Hi ,
> Looking at the reg expression ( $playercard1 =~ /10|J|Q|K/ ),
> it will match 0, 1, and '10' , J, Q, K
>
> What must I do so that it will match only '10' , 'J', 'Q', and 'K'
>
> Thanks


/10|J|Q|K/ will not match 0 or 1 alone. It will match only '10',
J,Q,K. Similar to the RegEx /10/.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com