First, something more specific than 'this isn't working at all', will help in diagnosing the problem...but here are some comments which may help:
Theuerkorn Johannes <[EMAIL PROTECTED]> writes: > Hi there, i have (a probably simple) Question on Arrays: > > I want to get some Data out of my SQL Databas. I try using the same Query not only >once and put the collected data in diffrent Arrays. > > > for (my $i=0;$i<= $count_SNR_LOG_data;$i++){ > print "$i\n"; > my $sth_RACT_LOG = $dbh->prepare ("select KEY_SNR,DRAWING_RE,RCODE_ID from >RACT_LOG where KEY_SNR='$KEY_SNR_SNR_LOG[$i]';"); First, the $dbh->prepare statement should go before the 'for', like so: ' my $sth_RACT_LOG = $dbh->prepare('select KEY_SNR, DRAWING_RE, RCODE_ID from RACT_LOG where KEY_SNR = ?'); for (my $i = 0; $i <= $count_SNR_LOG_data; $i++) { print "$i\n"; ' I've also used a placeholder value (the '?') - this takes care of quoting, and, with a good database module, will speed the queries significantly. Search `perldoc DBI` for info. Also, if you are looping through all of the elements in the '@KEY_SNR_SNR_LOG' array, and assuming they are unique, it would be better to use foreach: ' foreach my $elem (@KEY_SNR_SNR_LOG) { ' This way '$elem' gets the value of each element in the array. #--back to your code - > $sth_RACT_LOG->execute || > die "Kann Abfrage sth_RACT_LOG nicht ausfuehren: $DBI::errstr\n"; call execute with the value to replace the ? with - if you are using a foreach loop as above, that would be '$elem', if you stick with the for loop, it would be: ' $sth_RACT_LOG->execute($KEY_SNR_SNR_LOG[$i]) || die "something german-sounding"; ' > while (my @RACT_LOG_data=$sth_RACT_LOG->fetchrow_array){ > > $count_RACT_LOG_data++; > push our @KEY_SNR_RACT_LOG[$i], $RACT_LOG_data[0]; > push our @DRAWING_RE_RACT_LOG[$i], $RACT_LOG_data[1]; > push our @RCODE_ID_RACT_LOG[$i], $RACT_LOG_data[2]; > } There are a few problems with this: First, I would give each value it's own variable - an array just confusing things here: ' while (my ($key_snr, $drawing_re, $rcode_id) = $sth->fethrow_array) { ' Second, I don't see why 'our' is necessary - and if it is, I think the proper usage would be to call 'our @KEY_SNR_RACT_LOG' (etc) before the while loop. I've never really had a use for 'our', so perhaps someone else could shed more light on the situation. Third, assuming you were to switch to a 'foreach' (and still assuming all the element in '@KEY_SNR_SNR_LOG' are unique), use hashes: ' my %key_log; my %drawing_log; my %rcode_log; #somewhere above these loops # ... push @{$key_log{$elem}}, $key_snr; push @{$drawing_log{$elem}}, $drawing_re; push @{$rcode_log{$elem}}, $rcode_id; ' This says, basically: 'The value of the %key_log hash for key '$elem' is an array reference. Push '$key_snr' onto the end of that array.' Later, you would call the elements of the arrays like so: 'print $key_log{$elem}->[0]', or the whole array like 'my @foo = @{$key_log{$elem}}'. If you feel you must use arrays, what you probably mean to do is this: ' push @{$KEY_SNR_RACT_LOG[$i]}, $RACT_LOG_data[0]; ' Which says 'The value of element $i of array '@KEY_SNR_SNR_LOG' is an array reference, push $RACT_LOG_data[0] onto the end of that array. What you have up there is a slice. Read the Camel book for more information about them. For now, just use one of the methods I suggest above. Fourth, the variable '$count_RACT_LOG_data' is almost certainly useless for you, and definately useless for what you use it for below. You are counting the total number of times the query returned data - over all of the '$sth->executes'. > Thats how I try to get data out of my Arrays... > > for (my $i=0;$i<=$count_SNR_LOG_DATA,$i++){ > for (my $u=0;$u<=$count_RACT_LOG_data;$u++){ > print "$KEY_SNR_RACT_LOG[$i][$u]\n"; > print "$DRAWING_RE_RACT_LOG[$i][$u]\n"; > print "$RCODE_ID_RACT_LOG[$i][$u]\n"; > } > } As I said above, part of the problem is that '$count_RACT_LOG_data' contains the total number of times $sth->fetchrow returned data...which goes way out of bounds for each individual array. Re-written with hashes, as recommended in #3 above: ' foreach my $elem (@KEY_SNR_SNR_LOG) { for (my $u = 0; $u < @{$key_log{$elem}}; $u++) { print $key_log{$elem}->[$u] . "\n"; print $drawing_log{$elem}->[$u] . "\n"; print $rcode_log{$elem}->[$u] . "\n"; } } ' I've done some not-so-nice things here, and would probably rewrite the output part entirely, depending on how I needed to access the data, but this way is pretty much identical to your way, with hashes. The key for this part is not to use '$count_RACT_log' - use either the number of elements in the aray '$u < @KEY_SNR_RACT_LOG', or the index of the last element in the array '$u <= $#KEY_SNR_RACT_LOG' in the inner for loop. The second method is much more correct, actually. There are better ways to do this, but this post is getting long enough as it is. I would recommend a good look at the references and data structures chapters of 'Programming Perl'. They should help with understanding how to deal with more complicated data like this. Wow, that was a long post. Hope it made sense... All of this code is pretty much completely untested. Beware typos, logic errors, and gremlins. -RN -- Robin Norwood Red Hat, Inc. "The Sage does nothing, yet nothing remains undone." -Lao Tzu, Te Tao Ching -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]