Re: A second beginner's question

From: Lawrence Statton (lawrence_at_cluon.com)
Date: 01/14/05


To: "Doug Essinger-Hileman" <greypilgrim@interior-castle.org>
Date: Fri, 14 Jan 2005 09:42:41 -0800


> Now that I have the earlier snippet working (and thank you to all who
> helped), I am working on shuffling my array. Basically, the array is
> a list of names in the order they will receive a job assignment.
> Every third week, I want to shuffle the order. I have tested the
> basics of shuffling the array, and it works just fine. In this code
> snippet, my @group has already been defined:
>
> ---begin snippet---
> my @oddgroup = @group[1,3];
> my @evengroup = @group[0,2];
> my @newgroup = (@oddgroup, @evengroup);
>
> open CONTROL2, '>test.cont';
> print CONTROL2 "Group: @group\n";
> print CONTROL2 "OddGroup: @oddgroup\n";
> print CONTROL2 "EvenGroup: @evengroup\n";
> print CONTROL2 "NewGroup: @newgroup\n";
> close CONTROL2;
> ---end snippet---
>
> This works just fine. But when I add the mechanism to shuffle every
> third week, I have problems. The code, which has already set my
> @group and my $switch is
>
> ---begin snippet
> if ($switch % 3 == 0)
> {
> my @oddgroup = @group[1,3];
> my @evengroup = @group[0,2];
> my @newgroup = (@oddgroup, @evengroup);
> }
>
> else
> {my @newgroup = @group};
>
> open CONTROL2, '>test.cont';
> print CONTROL2 "Group: @group\n";
> print CONTROL2 "NewGroup: @newgroup\n";
> close CONTROL2;
> ---end snippet---
>
> When I run this, I get two error messages:
>
> Possible unintended interpolation of @newgroup
> Global symbol "@newgroup" requires explicit package name
>
> >From reading man perldiag, I understand that the last message is
> telling me that I either need to lexically scope @group, or declare
> it beforehand or explicitly qualify it. But haven't I already
> lexically scoped it? (My guess is that I haven't done that properly,
> since I'm getting the error message; but I cannot figure out what is
> wrong with my syntax.) And the other message I didn't find in
> perldiag.
>

You have a few TOO many 'my's ... your problem is all revolving around scoping.

In the sort snippet you say my @newgroup = (@oddgroup, @evengroup) and
then in the very next line destroy @newgroup by closing the block.

Using lexical variables is a good thing, but you don't just litter
your code with 'my' -- you have to declare each variable once in the
narrowest usable scope ...

my @group = qw / Fred Mary Lawrence Georgia / ;
my @newgroup = @group;
my $switch = ...whever this comes from...

if ( 0 == $switch % 3 )
{
  @newgroup = @group[1,3,0,2];
}

open CONTROL2, '>test.cont' or die "Could not create output file: $!";
print CONTROL2 "Group: @group\n";
print CONTROL2 "Newgroup: @newgroup\n";
close CONTROL2;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
        Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.



Relevant Pages

  • Re: Randomly outputting an array
    ... That was a stupid question, ... there are lots of ways of shuffling an array that ... for shuffling an array and I realized why it's structured exactly the ... Algorithm P given by Professor Knuth in Seminumerical Algorithms, ...
    (comp.programming)
  • Re: Random number again
    ... The "fill the array section" fills an array so that ... It basically randomly picks two items in the array and swaps them. ... With 12 numbers, doing 20 swaps should do a pretty good job shuffling, even if occasionally, the same slots got swapped. ... Dim raynumis just creating an array that can hold 12 different numbers. ...
    (microsoft.public.powerpoint)
  • Re: Q: Algorithm M vs. P of Knuths book
    ... > a PRNG) in some sense more random by shuffling it through ... > other hand, if one has an array filled with the numbers, ... > of the array elements with algorithm P and then output ... We might consider the entropy of an element's output position ...
    (sci.crypt)
  • Re: random permutation of an array
    ... The following is a generalized shuffling routine that I have posted in the past over in the compiled VB newsgroups, but it works fine in the VBA world of Excel as well. ... Give it an array of elements and it will put them in random order and return the randomized elements back in the original array that was passed to it. ... Static RanBefore As Boolean ...
    (microsoft.public.excel.programming)
  • Re: Newbee Question about random numbers
    ... Bob, you are almost there. ... Shuffling is not picking random numbers out of a hat -- you got that. ... This is a school project that I'm working on. ... > the words in array mixed up. ...
    (comp.lang.java.programmer)