Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Naming some arrays in a foreach loop?!

Reply
Thread Tools

Naming some arrays in a foreach loop?!

 
 
Thomas Becker
Guest
Posts: n/a
 
      07-07-2003
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
 
Reply With Quote
 
 
 
 
Greg Bacon
Guest
Posts: n/a
 
      07-07-2003
In article <(E-Mail Removed) >,
Thomas Becker <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Benjamin Goldberg
Guest
Posts: n/a
 
      07-07-2003


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;}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically naming arrays ccc31807 Perl Misc 7 02-02-2011 12:57 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Javax.naming Exception: name not found in naming service. Harman Java 1 07-28-2006 08:51 AM
foreach with two arrays Thomas P. Perl Misc 3 07-19-2005 02:46 PM



Advertisments