For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2004 > grabbing array vlaues in a loop where array is redefined









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author grabbing array vlaues in a loop where array is redefined
Derf

2004-06-28, 3:56 am

for($x=1;$x<=7;$x++){
$dbase="db".$x;
my $sqlget;
$sqlget = qq{select corrects from $dbase};

my $sthget = $dbh->prepare( $sqlget ) ||die "Couldn't prepare statement:
$DBI::errstr";

$sthget->execute() || die "Couldn't execute statement: $sthget->errstr";

while(my @data = $sthget->fetchrow_array()){
$data[0] = "N/A" unless defined $data[0];
push @correct,$dataws[0];
}

$sthget->finish();

$quest="c".$x;
$corrects{$quest}=\@correct;
foreach $yo(keys %corrects){print qq~Yo: $yo -> @{$corrects{$yo}}\n~;}
@correct=();
}

===========
I need to store the NEW elements of @correct as the next value of %
corrects whilst keeping the old values of %corrects intact. Currently it
destroys the old values of %corrects, replacing them with the current
elements of @correct for each key created, i.e.

Yo: c1 -> 7 3 9 8

Yo: c1 -> 9 1 42 4
Yo: c2 -> 9 1 42 4

Yo: c1 -> 88 7 2 53 4 5
Yo: c2 -> 88 7 2 53 4 5
Yo: c3 -> 88 7 2 53 4 5

Yo: c1 -> 0 23 4 6 22 3 7
Yo: c2 -> 0 23 4 6 22 3 7
Yo: c3 -> 0 23 4 6 22 3 7
Yo: c4 -> 0 23 4 6 22 3 7

whereas I want it to look like:

Yo: c1 -> 7 3 9 8

Yo: c1 -> 7 3 9 8
Yo: c2 -> 9 1 42 4

Yo: c1 -> 7 3 9 8
Yo: c2 -> 9 1 42 4
Yo: c3 -> 88 7 2 53 4 5

Yo: c1 -> 7 3 9 8
Yo: c2 -> 9 1 42 4
Yo: c3 -> 88 7 2 53 4 5
Yo: c4 -> 0 23 4 6 22 3 7

Help?

Thanks!

Derf
Jaap Karssenberg

2004-06-28, 8:58 am

I think your problem is that you use refrences to @correct, these keep
pointing to the _current_ values of @correct.

Try:
$corrects{$quest}=[@corrects];

instead of:
$corrects{$quest}=\@correct;

This creates an anonymous reference for each data set (and effectively
copies the old data set).

--
) ( Jaap Karssenberg || Pardus [Larus] | |0| |
: : http://pardus-larus.student.utwente.nl/~pardus | | |0|
) \ / ( |0|0|0|
",.*'*.," Proud owner of "Perl6 Essentials" 1st edition :) wannabe
Derf

2004-06-28, 4:09 pm

Jaap Karssenberg <j.g.karssenberg@student.utwente.nl> wrote in
news:20040628111445.0c2d6bee@Captain:

>
> Try:
> $corrects{$quest}=[@corrects];
>
> instead of:
> $corrects{$quest}=\@correct;
>


Yes, this is the answer. Thanks so much.

Derf
Brian McCauley

2004-06-30, 9:05 am

Jaap Karssenberg <j.g.karssenberg@student.utwente.nl> writes:

> I think your problem is that you use refrences to @correct, these keep
> pointing to the _current_ values of @correct.
>
> Try:
> $corrects{$quest}=[@corrects];
>
> instead of:
> $corrects{$quest}=\@correct;


~10% of the time that's the right solution.

~90% of the time the problem only exists because you've given @correct
the wrong scope.

You should always declare all variables as lexically scoped in the
smallest applicable lexical scope unless you have a positive reason to
do otherwise. BTW: this is not perculliar to Perl, it applies in all
programming languges - allowing that a language not having lexical
variables is a positive reason :-).

For Perl this means that most of the time the declaration of scalars
should be combined with the first assignment. BTW: this to is not
perculliar to Perl, it also applies in other programming languges
where assignment and declaration can be combined.

By following this convention you will be able to get maximum beniefit
out of putting "use strict" at the top of all your scripts.

Try to get into this habit now, do not wait for your failure to do so
to cause you the unecessary distress of wasting your own time and that
of other people. (Oops, too late). The longer you leave it the harder
you will find it to adjust. Worse still, if you leave it too long you
may never adjust and may mutate into a bitter and twisted troll.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com