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

Hi monks I am trying to create an image map by using Perl and JavaScript. I have got many points to use in my image map so i thought an implementation in Perl could help as i couldn't really tihnk of any other solution. Anyway here is the code:
## Previous code to find x coordinates, y coordinates ## and website names my @x_points; my @y_points; my @websites; my $secondcount; <IMG SRC="figure.gif" USEMAP="#mymap"> <MAP NAME="mymap"> for ($secondcount=0; $secondcount < $webpage_no; $secondcount++) { <AREA SHAPE=circle COORDS="'$x_points[$secondcount],$y_points[$se +condcount],1" HREF="'$websites[$secondcount]"> } </MAP>
The problem is that i get errors at the line where i have the for loop. The errors are:

syntax error near ++) and

Syntax error near "for"

Also, even if those two problems are solved do you reckon the code will do what i want? That is, for say $x_points[0], $y_points[0] and $websites[0] then the coordinates will be the first two variables and the website they link to will be the $websites[0] variable. Thanks for looking and your replies

Replies are listed 'Best First'.
Re: Creating an image map using Perl code
by rhesa (Vicar) on Apr 06, 2006 at 01:38 UTC
    You can't just mix code and data like that. Even PHP has markers to separate them :)

    Try printing your html:

    ## Previous code to find x coordinates, y coordinates ## and website names my @x_points; my @y_points; my @websites; my $secondcount; print q( <IMG SRC="figure.gif" USEMAP="#mymap"> <MAP NAME="mymap"> ); for ($secondcount=1; $secondcount <= $webpage_no; $secondcount++) { print qq( <AREA SHAPE=circle COORDS="'$x_points[$secondcount],$y_ +points[$secondcount],1" HREF="'$websites[$secondcount]"> ); } print q( </MAP> );
    Accessing an array element still needs a sigil: x_points[$secondcount] doesn't work, you meant to say $x_points[$secondcount].

    Oh, and why do you start counting at 1?

      Sorry those were silly mistakes. I still get the same errors now with the sigils though. And i ve started counting from 0 but have removed the = sign from the second argument in the for loop. Is there a way to mix the for loop and the JavaScript/HTML? I still can't run and print my algorithm as i still get the same two errors. Thanks
        The problem must be somewhere else in your code, because the snippet that I posted works (even though it doesn't print any <area> tags).

        Try starting your script with this:

        use strict; use warnings;
        Then try running your script through the perl compiler:  perl -c your_script.pl. That should give you a lot more information about possible causes for the errors.

        If that doesn't help you out, can you post a bit more code?

        Did you notice he also added prints, not just sigils? I see you've (silently) updated your node to add the sigils and to fix the loop bounds, but the prints are still missing.
Re: Creating an image map using Perl code
by Cody Pendant (Prior) on Apr 06, 2006 at 07:36 UTC
    This seems to work, although of course I have no idea what's in your data:
    #!/usr/bin/perl my @x_points = ( 100, 200 ); my @y_points = ( 100, 200 ); my @radiuses = ( 50, 50 ); my @websites = qw( http://www.yahoo.com http://www.google.com ); print q( <IMG SRC="figure.gif" USEMAP="#mymap" width="400" height="400"> <MAP NAME="mymap"> ); for ( my $i = 0 ; $i < @x_points ; $i++ ) { print qq(<AREA SHAPE="circle" COORDS="$x_points[$i],$y_points[$i],$radiuses[$i]" HREF="$websites[$i]"> ); } print q(</MAP>);
    This produces two circular links, centres at 100,100 and 200,200, with radius 50. Very roughly, something like this:
    ###################################################### # # # # # ###### # # ## ## # # # # # # # # <-yahoo # # # # # # ## ## # # ###### # # ###### # # ## ## # # # # # # # # <- google # # # # # # ## ## # # ###### # # # # # # # # # # # # # # # # # # # # # ######################################################
    But what your code is crying out for is a nice little AoH containing all the data in a much better structure. Exercise for the reader etc.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
      Right, i have ran Cody Pendant's algorithm and it works. My algorithm has similar data but it gives the same errors:
      #!/usr/bin/perl use warnings; use strict; use DBIx::Simple; my $db = DBIx::Simple->connect('dbi:SQLite:dbname=mydatabase.db') or die DBIx::Simple->error; open (X_POINTS,"x_points.txt") || die "couldn't open the file!"; open (Y_POINTS,"y_points.txt") || die "couldn't open the file!"; # Insert all the x points in an array my @x_points = <X_POINTS>; # Insert all the y points in an array my @y_points = <Y_POINTS>; close(X_POINTS); close(Y_POINTS); print @x_points; print @y_points; my @webpages; my $webpage_no = 500; for (my $count=0; $count <= $webpage_no; $count++) { $webpages[$count] = $db->query("SELECT webpage FROM webpages_dat +a WHERE id = '$count'")->list; } print @webpages; print q(<IMG SRC="figure.gif" USEMAP="#mymap"> <MAP NAME="mymap"> ) for (my $secondcount=0; $secondcount <= $webpage_no; $secondcount++) +{ print qq(<AREA SHAPE=circle COORDS="$x_points[$secondcount],$y_po +ints[$secondcount],1" HREF="$webpages[$secondcount]">); } print q(</MAP>); ******************************************
      This is the complete code. The print statements work for the coordinates and the webpages so that's fine. The errors are:

      syntax error at image_map_gui.pl line 49, near "0;"

      syntax error at image_map_gui.pl line 49, near "++) "

      Execution of image_map_gui.pl aborted due to compilation errors. Can't really understand what's the problem here. If i comment out the JavaScript and the for loop bit it works fine but if i add those it gives the errors. Any ideas? thanks for your help

        You're missing a semicolon after your first print q(...) statement.
Re: Creating an image map using Perl code
by Cody Pendant (Prior) on Apr 06, 2006 at 07:25 UTC
    Not strictly Perl related but you don't actually want X and Y coordinates if your AREA tag is a circle. You have X and Y coordinates for the centre of the circle, and a pixel number for the radius.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: Creating an image map using Perl code
by TedPride (Priest) on Apr 06, 2006 at 03:29 UTC
    How are you getting the coordinates from Javascript to Perl? That's the part that always confused me.