Naming some arrays in a foreach loop?!

Discussion in 'Perl Misc' started by Thomas Becker, Jul 7, 2003.

  1. 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
     
    Thomas Becker, Jul 7, 2003
    #1
    1. Advertising

  2. Thomas Becker

    Greg Bacon Guest

    In article <>,
    Thomas Becker <> 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
     
    Greg Bacon, Jul 7, 2003
    #2
    1. Advertising

  3. 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_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.)

    > 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;}
     
    Benjamin Goldberg, Jul 7, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    366
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    386
    Luc Kumps
    Aug 22, 2003
  3. Harman
    Replies:
    1
    Views:
    2,525
    Moiristo
    Jul 28, 2006
  4. Thomas P.

    foreach with two arrays

    Thomas P., Jul 19, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    167
    Fabian Pilkowski
    Jul 19, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    398
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page