On 2014-02-24, Marek Novotny <(E-Mail Removed)> wrote:
> Hi group,
>
> Tonight I finished up another obj for my class. The class has not yet
> started to learn about use strict; and use warnings;
Maybe they haven't taught you yet, but they are best practice, and
will catch most errors, it saves the regulars here from pointing out
the obvious.
Use them, and then strip them out before your tutor sees your code.
I've never heard of removing them breaking any code. [1]
> I was given the following to solve:
[snipped]
Have they taught you about random numbers yet? I can't see how it's
possible to shuffle without random numbers.
However, you've been told to use pop, shift and push. Maybe you could
create a new array, pushing cards into it that you've 'pop'ped and
'shift'ed from the old array in some pseudo random fashion? [2]
[snipped]
> When I started to try to use the use strict and warnings, I got warnings
> such as: Scalar value @diamonds[$i] better written as $diamonds[$i] at ./
> obj10.pl line 55.
>
> But the whole point of what I am doing is loading elements into an
> array... So I wanted it as @diamonds, not $diamonds, and so on.
@diamonds is the whole array, $diamonds[2] refers an array element.
(the same works for hashes, but you probably haven't done those yet:
%coffee is the whole hash, but $coffee{espresso} refers to a portion
of data *in* the %coffee hash).
The @ and % refer to the whole. $ and [suffix] or {suffix} refer to
a part for arrays and hashes respectively.
> I tried to make the code as simple to understand as possible.
>
> Anyone care to take a stab at what it would have to look like with the
> use of strict and warnings so I can understand what I should be doing?
That's a lot of work for grabbing the wrong end of the stick. :(
Justin.
1. except:
no warnings 'uninitialized';
# and similar
but you won't be using that without having been taught about 'use
warnings' anyway).
2. Maybe something like:
for (my $i = 0; $i < $size_of_array; $i++) {
if ($i % 3) {
push @new, shift @array;
}
else {
push @new, pop @array;
}
}
You might want to wrap all of that in another loop and run it a
few times. It should give *the appearance* of a shuffled deck,
but you can see it'll shuffle identically each time, so it's not
a real shuffle - if running it more than once be sure to replace
@array with @new each time or you'll shuffle the first array
each time, and not the partly shuffled one!
--
Justin C, by the sea.
> Hi group,
>
> Tonight I finished up another obj for my class. The class has not yet
> started to learn about use strict; and use warnings;
Maybe they haven't taught you yet, but they are best practice, and
will catch most errors, it saves the regulars here from pointing out
the obvious.
Use them, and then strip them out before your tutor sees your code.
I've never heard of removing them breaking any code. [1]
> I was given the following to solve:
[snipped]
Have they taught you about random numbers yet? I can't see how it's
possible to shuffle without random numbers.
However, you've been told to use pop, shift and push. Maybe you could
create a new array, pushing cards into it that you've 'pop'ped and
'shift'ed from the old array in some pseudo random fashion? [2]
[snipped]
> When I started to try to use the use strict and warnings, I got warnings
> such as: Scalar value @diamonds[$i] better written as $diamonds[$i] at ./
> obj10.pl line 55.
>
> But the whole point of what I am doing is loading elements into an
> array... So I wanted it as @diamonds, not $diamonds, and so on.
@diamonds is the whole array, $diamonds[2] refers an array element.
(the same works for hashes, but you probably haven't done those yet:
%coffee is the whole hash, but $coffee{espresso} refers to a portion
of data *in* the %coffee hash).
The @ and % refer to the whole. $ and [suffix] or {suffix} refer to
a part for arrays and hashes respectively.
> I tried to make the code as simple to understand as possible.
>
> Anyone care to take a stab at what it would have to look like with the
> use of strict and warnings so I can understand what I should be doing?
That's a lot of work for grabbing the wrong end of the stick. :(
Justin.
1. except:
no warnings 'uninitialized';
# and similar
but you won't be using that without having been taught about 'use
warnings' anyway).
2. Maybe something like:
for (my $i = 0; $i < $size_of_array; $i++) {
if ($i % 3) {
push @new, shift @array;
}
else {
push @new, pop @array;
}
}
You might want to wrap all of that in another loop and run it a
few times. It should give *the appearance* of a shuffled deck,
but you can see it'll shuffle identically each time, so it's not
a real shuffle - if running it more than once be sure to replace
@array with @new each time or you'll shuffle the first array
each time, and not the partly shuffled one!
--
Justin C, by the sea.
Aucun commentaire:
Enregistrer un commentaire