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

Hi monks, Wondered if you could help -

I have a data-file which has numerical information on up to 96 different positions on a grid. e.g no 12 may be missing etc but the scale is from 1-96.

I am iterating through these lines in a foreach loop, extracting the grid reference number (1-96) and computing a clickable image leading to graphs etc for each position.

The problem is occuring when a particular number is missing, e.g if number 12 is missing graphs are produced for 1-95, not 1-11 and 13-96, meaning the grid number does not correspond to the graph created. I have tried using counters etc to get around this problem but nothing i working - I wondered if anyone had a simple logical way to get around this. Thankyou.

foreach my $line (@grid) { $line =~ s/^(\d+){1,2}//; $grid_no = $1; # @numbers and @data irrelevent $plot = plot_graph (\@numbers, \@data, $grid_no); 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">); ## etc ..........

Replies are listed 'Best First'.
Re: Working with values that dont exist
by broquaint (Abbot) on May 29, 2003 at 10:09 UTC
    It will be simpler if you just start out with a 96 element array and then go from there e.g
    my @points; @points[1 .. 96] = (undef) x 96; for (@grid) { /^(\d{1,2})$/ and $points[$1] = 1; ... }
    So at the end of your for loop your @points array will have 96 elements where their value will either be true or false. Also note the change in the regex as yours was somewhat broken (it tried to match 1-2 captures of one or more \d).
    HTH

    _________
    broquaint

      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">);
        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

        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.
Re: Working with values that dont exist
by fglock (Vicar) on May 29, 2003 at 12:40 UTC

    I don't think I understand your code very well, but it looks like you could try this:

          $graph_links[ $grid_no ] = $plot . ' ';

    instead of:

          push @graph_links, $plot . ' ';

    update: but when you do this you are removing the positional info anyway:

    my $graph_links = join ('', @graph_links); @graph_links = split (/\s+/, $graph_links);