in reply to Re: Working with values that dont exist
in thread Working with values that dont exist

Hi broquaint,

Thankyou for your advice but i cant quite get your code to fit in with what i'm doing.. I really need to be able to generate each graph within the foreach loop (as the values for the graph change with each grid reference).

my @points; @points[1 .. 96] = (undef) x 96; foreach my $line (@grid) { # lots and lots of other code here that I dont want to c +hange. /^(\d{1,2})$/ and $points[$1] = 1; # this doesn't work for (my $i = 1; $i < @points; $i++) { $plot = plot_graph (\@temps, \@new); push @graph_links, $plot . ' '; } } my $graph_links = join ('', @graph_links); @graph_links = split (/\s+/, $graph_links); print qq(<P>Please click on a spot! <P>); print qq(<P><div align=center><img src=array.bmp border=0 usemap=#arra +y><map name=array><P>); print qq(<area href="http://biolarfs1/~khh103/cgi-bin/$graph_links[0]" + ALT="" shape="circle" coords="13,9,6">);

Replies are listed 'Best First'.
Re: Re: Re: Working with values that dont exist
by broquaint (Abbot) on May 29, 2003 at 13:44 UTC
    Might I ask where you found or were inspired by this code?
    my $graph_links = join ('', @graph_links); @graph_links = split (/\s+/, $graph_links);
    As I've seen it here before and it's really rather backward code. What you're doing is joining all the elements of @graph_links on a empty delimiter to produce a string, and then splitting the string on all the whitespace to produce @graph_links without whitespace. It would make much more sense to just leave out the whitespace in the first place (in your particular case) and completely skip that.

    Also, what are you trying to achieve in that inner loop? All you're doing is looping 96 time and pushing the results of plot_graph() onto @graph_links plus some extraneous whitespace, which makes the @points array a little redundant. Perhaps you mean to create the @graph_links array like this

    ## don't bother with @points as it looks to be redundant push @graph_links, ( /^(\d{1,2})$/ ? plot_graph (\@temps, \@new, $1) : undef );
    Then when you iterate over the @graph_links array later on, skip any elements that aren't defined e.g
    for my $p (@graph_links) { next unless defined $p; print ... }
    This way any undefined elements will be skipped so you should get the effect of 1-11, 13-96.
    HTH

    _________
    broquaint

Re: Re: Re: Working with values that dont exist
by hangmanto (Monk) on May 29, 2003 at 12:40 UTC
    I'm not sure I can help with the overall problem, but I do have some suggestions.

    change the regex to use the $line variable instead of the default $_.

    $line =~ /^(\d{1,2})$/; $points[$1] = 1;
    In place of your for loop, I would suggest using a simple foreach. You don't seem to be using the $i variable anyway. If you want to keep the for loop, you'll need to change it to
    for ( my $i = 0; $i < scalar @points; $i++ )
    I hope this helps.