List Info

Thread: Re: random subscripts




Re: random subscripts
country flaguser name
United States
2008-03-19 23:56:16


--- merlyn%40stonehenge.com">merlynstonehenge.com wrote:

> >>>>> "Donald" == Donald Korrecta
> < draktrax%40yahoo.com">draktraxyahoo.com> writes:
>
> Donald> I can get ten cards but, they are all the
> same card.
&gt;
> Your code wouldn't get the same card each time.
> Maybe you mean
>; "sometimes I get the same card".
>
> Unless somehow you have only one card in deck.
&gt;
> So perhaps you could post the code that has the
> behavior above,
&gt; or confirm the other two things I just said. Also
>; show
>; how you initialize deck.
&gt;
> --
> Randal L. Schwartz - Stonehenge Consulting Services,
> Inc. - +1 503 777 0095
>; < merlyn%40stonehenge.com">merlynstonehenge.com>
> <URL:http://www.stonehenge.com/merlyn/>
&gt; Perl/Unix/security consulting, Technical writing,
> Comedy, etc. etc.
>; See PerlTraining.Stonehenge.com for onsite and
> open-enrollment Perl training!
>
The entire prog:
foreach $b ("spades", "hearts", "clubs", "diamonds") {
foreach $a ("ace", 2..10, "jack", "queen", "king") {
$c=0;
$deck[&;c]= "$a of $b";
$c++;
}
}
foreach $i (1..10) }
$card=$deck[rand deck];
print "Card $i is the $card;
}

if i print out the result of the nested loops
i get a list of all cards. the prog returns only the
king of diamonds. what am i missing?

__________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs

__._,_.___
.

__,_._,___
Re: random subscripts
country flaguser name
United States
2008-03-20 00:51:38

Move that initialization"$c = 0;" before the foreach loop. Every time index
'1' is getting initialized and finally that array is having only one element
whch is initialized at last

-Yaswanth

On Thu, Mar 20, 2008 at 10:26 AM, Donald Korrecta < draktrax%40yahoo.com">draktraxyahoo.com>
wrote:

&gt;
> --- merlyn%40stonehenge.com">merlynstonehenge.com <merlyn%40stonehenge.com> wrote:
&gt;
> > >>&gt;>> "Donald" == Donald Korrecta
> > < draktrax%40yahoo.com">draktraxyahoo.com <draktrax%40yahoo.com>>; writes:
> >
>; > Donald> I can get ten cards but, they are all the
> > same card.
&gt; >
>; > Your code wouldn't get the same card each time.
&gt; > Maybe you mean
>; > "sometimes I get the same card".
> >
>; > Unless somehow you have only one card in deck.
&gt; >
>; > So perhaps you could post the code that has the
> > behavior above,
&gt; > or confirm the other two things I just said. Also
>; > show
>; > how you initialize deck.
&gt; >
>; > --
> > Randal L. Schwartz - Stonehenge Consulting Services,
> > Inc. - +1 503 777 0095
>; > < merlyn%40stonehenge.com">merlynstonehenge.com <merlyn%40stonehenge.com>>
> > <URL:http://www.stonehenge.com/merlyn/>
&gt; > Perl/Unix/security consulting, Technical writing,
> > Comedy, etc. etc.
>; > See PerlTraining.Stonehenge.com for onsite and
> > open-enrollment Perl training!
> >
>; The entire prog:
&gt; foreach $b ("spades", "hearts", "clubs", "diamonds") {
> foreach $a ("ace", 2..10, "jack", "queen", "king") {
> $c=0;
&gt; $deck[&;c]= "$a of $b";
> $c++;
> }
> }
> foreach $i (1..10) }
> $card=$deck[rand deck];
&gt; print "Card $i is the $card;
&gt; }
>
> if i print out the result of the nested loops
&gt; i get a list of all cards. the prog returns only the
> king of diamonds. what am i missing?
>
>; __________________________________________________________
> Never miss a thing. Make Yahoo your home page.
&gt; http://www.yahoo.com/r/hs
&gt;
>

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: random subscripts
country flaguser name
Czech Republic
2008-03-20 06:34:51

From: Donald Korrecta < draktrax%40yahoo.com">draktraxyahoo.com>
>; The entire prog:
&gt; foreach $b ("spades", "hearts", "clubs", "diamonds") {
> foreach $a ("ace", 2..10, "jack", "queen", "king") {
> $c=0;
&gt; $deck[&;c]= "$a of $b";

I believe you meant
$deck[$c]= "$a of $b";

your code would attempt to call the c() subroutine and thus should
return an error that there is no such function.

> $c++;
> }
> }
> foreach $i (1..10) }
> $card=$deck[rand deck];
&gt; print "Card $i is the $card;
&gt; }
>
> if i print out the result of the nested loops
&gt; i get a list of all cards. the prog returns only the
> king of diamonds. what am i missing?

The first thing you are missing is

use strict;
use warnings;
# and possibly
# no warnings 'uninitialized'; # I find this particular warning
annoying.

then you are missing the variable declarations.

foreach my $b ("spades", "hearts", "clubs", "diamonds") {
foreach my $a ("ace", 2..10, "jack", "queen", "king") {
my $c=0;
$deck[$c]= "$a of $b";
$c++;
}
}
foreach my $i (1..10) }
my $card=$deck[rand deck];
print "Card $i is the $cardn&quot;;
}

this might help you to see the problem, you reset the $c counter in
each iteration of the inner foreach loop, thus you only ever set
$deck[0]. Which you could easiy find out by printing the deck to
make sure it's initialized correctly:

use Data:umper;
print Dumper(deck);

Next thing, you should not use variables $a and $b outside sort{}.
They are a bit special. Besides $a and $b is not very informative, is
it? What does "a&quot; mean? Nothing! Use meaningful variable names!

Also, if you want to add a value at the end of an array you do not
need to keep a counter, just push() the value into the array:

use strict;
use warnings;
no warnings 'uninitialized';

my deck;
foreach my $color ("spades", "hearts", "clubs", "diamonds") {
foreach my $value ("ace", 2..10, "jack", "queen", "king") {
push deck, "$value of $color&quot;;
}
}
# use Data:umper;
# print Dumper(deck);

foreach my $i (1..10) }
my $card=$deck[rand deck];
print "Card $i is the $cardn&quot;;
}

HTH, Jenda
===== Jenda%40Krynicky.cz">JendaKrynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )