bhu73 has asked for the wisdom of the Perl Monks concerning the following question:

http://62.5.7.15/code.txt contains the full code but basically :

$groups{'de'}{'kista_103'} = 'spray.net_health_check.txt';
$groups{'de'}{'kista_118'} = 'spray.net_search_?form=norm';
$groups{'dpapache'}{'kista_136'} = 'dpapache02.spray.net.txt';
$groups{'dpapache'}{'kista_137'} = 'dpapache01.spray.net.txt';

# Static vars

$graphdir='/home/mark/graphs';
$rrddir='/home/mark/rrd';


foreach $group ( sort keys %groups ) {
for $ds ( sort keys %{$groups{$group}} ) {
foreach $interval ("-2h","-2d","-2w"){
@rrdgraph1=();
@rrdgraph2=@rrdgraph1;
push @rrdgraph2,$pngtot;
@rrdgraph0=();
push @rrdgraph0,$def;
push @rrdgraph0,$line;
push @rrdgraph2,@rrdgraph0;
@rrdgraph3=@rrdgraph1;
$png= join("",$graphdir,"/",$ds,$interval,".png");
push @rrdgraph3,$png,@rrdgraph0;
$blank = shift @rrdgraph3;
}
}

exit;
<CODE> I expected that @rrdgraph2 would be populated with all of the $def and $line values for every value within the ds loop but it ends up the same as rrdgraph3

Replies are listed 'Best First'.
Re: why doesn't my array get extended?
by davorg (Chancellor) on May 04, 2001 at 12:33 UTC

    Each time round your inner loop you reset @rrdgraph2 to an empty list with the following two lines:

    @rrdgraph1=(); @rrdgraph2=@rrdgraph1;

    That may well be your problem.

    I can't access the full script, but it looks to me as tho' you don't use -w or use strict. These would force you to think more carefully about the scope of your variables and would therefore help you with problems like this.

    Oh, and one other thing. The line

    $png= join("",$graphdir,"/",$ds,$interval,".png");

    would be much easier to understand as

    $png= "$graphdir/$ds$interval.png";
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      yes, its gone now but thank you....I am very new to perl so all advice is welcome.....this strict and -w stuff bears closer examination....but I have a lord and master who doesnt bother with that sort of thing........gulp

        Then your lord and master is a fool and doesn't deserve your respect. You should sent him over here for some education.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: why doesn't my array get extended?
by bhu73 (Initiate) on May 04, 2001 at 12:46 UTC
    because I am stupid and resetting rrdgraph2 inside the loop
Re: why doesn't my array get extended?
by Sinister (Friar) on May 04, 2001 at 11:41 UTC
    By doing push @rrdgraph2, @rrdgraph0 you make the graph0 array as a whole an element of the graph2 array. You do not assign the values of the graph0 array to the graph2 array.
    To do such could do:
    foreach (@rrdgraph0) { push @rrdgraph2, $_; }

    Now you push every element from graph0 to graph2
    (I think this is what you meant...)

    Sinister greetings.

      Er... this seems to contradict what perldoc -f push has to say on the subject...

      push ARRAY,LIST Treats ARRAY as a stack, and pushes the values of LIST onto the end of ARRAY. The length of ARRAY increases by the length of LIST. Has the same effect as for $value (LIST) { $ARRAY[++$#ARRAY] = $value; } but is more efficient. Returns the new number of elements in the array.
      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

      That turns out not to be the case. To "make the graph0 array as a whole an element of the graph2 array", one would use something like push @rrdgraph2, \@rrdgraph0. push @rrdgraph2, @rrdgraph0 does the same thing as your foreach loop, but is more efficient.

      I suggest reading perldsc at least so that you understand why your solution is incorrect.

      hdp.