Re: A second beginner's question
From: Lawrence Statton (lawrence_at_cluon.com)
Date: 01/14/05
- Next message: John W. Krahn: "Re: A second beginner's question"
- Previous message: "Wagner: "RE: renaming a file"
- Maybe in reply to: Doug Essinger-Hileman: "A second beginner's question"
- Next in thread: John W. Krahn: "Re: A second beginner's question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: John W. Krahn: "Re: A second beginner's question"
- Previous message: "Wagner: "RE: renaming a file"
- Maybe in reply to: Doug Essinger-Hileman: "A second beginner's question"
- Next in thread: John W. Krahn: "Re: A second beginner's question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|