Naming some arrays in a foreach loop?!

T

Thomas Becker

Hey guys,

I've to read out some rows of some mysql db. No problem at all.

But I've to seperate the results for different machines.
So I tried the following:

while (@data = $sth->fetchrow_array()){
push @$i_date, $data[0];
push @$i_totalmem, $data[1];
push @$i_residentmem, $data[2];
push @$i_sharedmem, $data[3];
push @$i_privatemem, $data[4];
push @$i_cpu, $data[5];
push @$i_connections, $data[6];
}

Surely it does not work. :( How can I give the arrays names containing
$i?

And then I want to create some Graphs using GD:Graph (nice module).
How can I access every of the arrays created above seperately?

Again I tried something stupid:

foreach $i (@welo){
$my_graph->set_legend("$i_totalmem","$i_residentmem","$i_sharedmem","$i_privatemem","$i_cpu","$i_connections");
$gd = $my_graph->plot([\@$i_date, \@$i_totalmem,
\@$i_residentmem, \@$i_sharedmem, \@$i_privatemem, \@$i_cpu,
\@$i_connections])
or die $my_graph->error;
}

Surely it doesn't work at all. :)

Can someone give me a clue? I do not get this done by myself. I've got
no clue.

Thx ALOT, Thomas
 
G

Greg Bacon

: [...]
: So I tried the following:
:
: while (@data = $sth->fetchrow_array()){
: push @$i_date, $data[0];
: [...]
: }
:
: Surely it does not work. :( How can I give the arrays names containing
: $i?

The feature you're after is called a symbolic reference in Perl,
and you'd write that push as

push @{$i . "_date"}, $data[0];

However, YOU DON'T WANT TO DO THAT!!!

See "Why it's stupid to `use a variable as a variable name'" at

http://perl.plover.com/varvarname.html

and also the following resources:

http://perl.plover.com/varvarname2.html

http://perl.plover.com/varvarname3.html

: [...]

Greg
 
B

Benjamin Goldberg

Thomas said:
Hey guys,

I've to read out some rows of some mysql db. No problem at all.

But I've to seperate the results for different machines.
So I tried the following:

while (@data = $sth->fetchrow_array()){
push @$i_date, $data[0];
push @$i_totalmem, $data[1];
push @$i_residentmem, $data[2];
push @$i_sharedmem, $data[3];
push @$i_privatemem, $data[4];
push @$i_cpu, $data[5];
push @$i_connections, $data[6];
}

Surely it does not work. :( How can I give the arrays names containing
$i?

Try this:

while( my @row = $sth->fetchrow_array ) {
push @{$data[$i]{$_}}, shift @row for qw(
date totalmem residentmem sharedmem cpu connections);
}

Then, look in $data[$i]{date} for what you are calling $i_date, etc..

It would probably be safer to do:

$sth->{FetchHashKeyName} = "NAME_lc";
while( my $row = $sth->fetchrow_hashref ) {
push @{$data[$i]{$_}}, $row->{$_} for qw(
date totalmem residentmem sharedmem cpu connections);
}

So that if the order of columns in the table changes, your code won't
get confused.
And then I want to create some Graphs using GD:Graph (nice module).
How can I access every of the arrays created above seperately?

Again I tried something stupid:

foreach $i (@welo){
$my_graph->set_legend("$i_totalmem","$i_residentmem",
"$i_sharedmem","$i_privatemem","$i_cpu","$i_connections");
$gd = $my_graph->plot([\@$i_date, \@$i_totalmem,
\@$i_residentmem, \@$i_sharedmem, \@$i_privatemem, \@$i_cpu,
\@$i_connections])
or die $my_graph->error;
}

foreach $i (@welo) {
my @graph_this = qw(
date totalmem residentmem sharedmem cpu connections);

my @labels = map "${i}_$_", @graph_this[1..$#graph_this];
$my_graph->set_legend( @labels );

my @data = @{$data[$i]}{@graph_this};
my $gd = $my_graph->plot( \@data )
or die $my_graph->error;
}
Surely it doesn't work at all. :)
Can someone give me a clue?

You're looking for symbolic references, but don't do that.

http://perl.plover.com/varvarname.html
http://perl.plover.com/varvarname2.html
http://perl.plover.com/varvarname3.html

Use an @data array instead. (Or maybe, a %data hash, and $data{$i}
instead of $data[$i]. It depends on what types of values $i will be.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top