Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Naming some arrays in a foreach loop?! (http://www.velocityreviews.com/forums/t869968-naming-some-arrays-in-a-foreach-loop.html)

Thomas Becker 07-07-2003 01:58 PM

Naming some arrays in a foreach loop?!
 
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_sha redmem","$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

Greg Bacon 07-07-2003 02:55 PM

Re: Naming some arrays in a foreach loop?!
 
In article <66b20d2c.0307070558.7f0a8291@posting.google.com >,
Thomas Becker <ashaman@creative-dark.de> wrote:

: [...]
: 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
--
You have to choose between trusting the natural stability of gold and the
honesty and intelligence of members of the government. With due respect for
these gentlemen, I advise you, as long as the capitalist system lasts, to
vote for gold. -- George Bernard Shaw

Benjamin Goldberg 07-07-2003 10:42 PM

Re: Naming some arrays in a foreach loop?!
 


Thomas Becker wrote:
>
> 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_connec tions");
> $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.)

> I do not get this done by myself. I've got no clue.
>
> Thx ALOT, Thomas


--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


All times are GMT. The time now is 12:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.