in reply to Re: Re: Re: pushing into arrays of arrays
in thread pushing into arrays of arrays

Hmm..,

I'm using the following to read through all of the pushed arrays and list their lengths, but it is returning length -1 each time..
foreach $ary (@arrays) { print $#{ary}; }


Grr. :)

Replies are listed 'Best First'.
Re: pushing into arrays of arrays
by Tomte (Priest) on Mar 05, 2004 at 13:08 UTC

    1t Note: $#array is not the length, but the biggest index.

    2nd note:use strict

    #!/usr/bin/env perl use strict; my $ar1 = [1,2,3]; my $ar2 = [4,5,6,7]; my @arrays = ($ar1,$ar2); foreach my $ary (@arrays) { print $#{ary}, "\n"; } __END__ Global symbol "@ary" requires explicit package name at test.pl line 10 +. Execution of test.pl aborted due to compilation errors.

    3d note: Read perldata and perlref! Really, that helps a lot!

    4th note:

    #!/usr/bin/env perl use strict; use warnings; my $ar1 = [1,2,3]; my $ar2 = [4,5,6,7]; my @arrays = ($ar1,$ar2); foreach my $ary (@arrays) { # correct syntax print $#{@$ary}, "\n"; # correct length print scalar(@$ary), "\n"; }

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      Thanks for that, it helped a lot; but I'm in a bit of a confused pickle.

      I'm using GD::Graph to draw some graphs. GD::Graph expects the data in the form;
      @graphdata=([@array_of_xlabels],[@dataset],[@dataset],other datasets.. +...)

      Now, I am trying to do this;
      @labels=(array,of,labels) loop loop build @dataset end loop push(@sets,[@dataset]) end loop foreach $se (@sets) @set=$#{$se} ((some processing)) push(@fixed,[@set]) end foreach push(@graphdata,[@labels]) foreach $fi (@fixed) @fixd=$#{$fi} push(@graphdata,[@fixd]) end foreach

      with the aim of ending up with @graphdata looking exactly the same as if I had done;
      @graphdata=([@labels],[@dataset],[@dataset2])

      so, pointers and references and whatnots all over the shop, and I'm getting confused.....

        If I'm not mistaken (I didn't run any code), you are pushing lists with one element (namely the bigest index of the list you IMO intend to push) onto @fixed.

        After that you push a lot of arrays containing only 1 (that is: (1,)) onto @graphdata.

        please use strict!

        try something like this, take the time to notice the differences between your example and this one:

        use strict; my @labels = qw(my one word labels); my @graphdata = (\@labels); my @sets = (); # looping and data building, fillings @sets foreach my $set (@sets) { my $se = []; # process $set into $se push @graphdata, $se; } my $graph; # GD::Graph stuff my $gd = $graph->plot(\@graphdata);

        Try to understand what you are doing, read a lot, debug your code (e.g. use Data::Dumper), read perldata and perlref thoroughly ;-)

        regards,
        tomte


        Hlade's Law:

        If you have a difficult task, give it to a lazy person --
        they will find an easier way to do it.