Stephen York <[EMAIL PROTECTED]> wrote:

: Hi,
:
: I've been trying to insert an array ref within a hash. I wrote a
: test program which was successful, but my real program requires
: the redefinition of the arrays to be done within an if block.
: When done in the if block it overwrites previous values. When
: done outside the if block a new array is created.
:
: I'm assuming that because I'm redeclaring within the if block
: this new array doesn't only has scope within the if block
: therefore the outer one hasn't been redefined.
:
: Here's the code that works:

    I assume you are starting with these:

use strict;
use warnings;


: my(%details,%all_details,$index,@stFrame,@eFrame);
:
: $stFrame[0] = 6;
: $stFrame[1] = 14;
: $stFrame[2] = 18;
: $stFrame[3] = 31;
: $stFrame[4] = 36;
:
: $eFrame[0] = 13;
: $eFrame[1] = 17;
: $eFrame[2] = 30;
: $eFrame[3] = 35;
: $eFrame[4] = 43;
:
: $details{'start'} = [EMAIL PROTECTED];
: $details{'finish'} = [EMAIL PROTECTED];
: $details{'data'} = "Data 1";
:
: $all_details{96} = \%details;

    Let's fix this first. Stop declaring your variables at the top
of your blocks. Declare them on the first instance of their use.

my @stFrame = ( 6, 14, 18, 31, 36 );
my @eFrame  = ( 13, 17, 30, 35, 43 );


my %details = (
    start   => [EMAIL PROTECTED],
    finish  => [EMAIL PROTECTED],
    data    => 'Data 1',
);

my %all_details = ( 96 => \%details );


: ############################
: my(%details,@stFrame,@eFrame);

    This should raise errors. You can't use my on a variable which
is already in scope.

TEST:

#!/usr/bin/perl

use strict;
use warnings;

my(%details,%all_details,$index,@stFrame,@eFrame);

my(%details,@stFrame,@eFrame);

__END__

Prints:

"my" variable %details masks earlier declaration in same scope at ...
"my" variable @stFrame masks earlier declaration in same scope at ...
"my" variable @eFrame masks earlier declaration in same scope at ...



: $stFrame[0] = 10;
: $stFrame[1] = 11;
: $stFrame[2] = 39;
: $stFrame[3] = 89;
: $stFrame[4] = 130;
:
: $eFrame[0] = 10;
: $eFrame[1] = 38;
: $eFrame[2] = 88;
: $eFrame[3] = 129;
: $eFrame[4] = 149;
:
: $details{'start'} = [EMAIL PROTECTED];
: $details{'finish'} = [EMAIL PROTECTED];
: $details{'data'} = "Data 2";
:
: $all_details{97} = \%details;

    It appears you are not using "warnings". That's a big part of
the problem you are having. This second half redefines the values
of %details, @stFrame, and @eFrame. You can test this by adding
these three lines to test the code so far.

use Data::Dumper 'Dumper';

print Dumper \( %details, @stFrame, @eFrame );

__END__


    All the code so far is a very long winded way of  as writing
this, except that $index has not been scoped.

#!/usr/bin/perl

use strict;
#use warnings;

my @stFrame = ( 10, 11, 39,  89, 139 );
my @eFrame  = ( 10, 38, 88, 129, 149 );


my %details = (
    start   => [EMAIL PROTECTED],
    finish  => [EMAIL PROTECTED],
    data    => 'Data 1',
);

my %all_details = ( 96 => \%details );


: ***************************************************************
: And here's the code with issues:

    I tried to look at the rest of this, but since you didn't tell
us what you are doing or working on, I couldn't understand what you
are trying to accomplish. Why not tell us the subject matter and
what results you are expecting. You have done a lot of work here,
but scoping and variable declaration are giving you a hard time.

    Before continuing you might want to read Coping with Scoping
by MJD. http://perl.plover.com/FAQs/Namespaces.html

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328

-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.3 - Release Date: 1/31/2005
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to