Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Code not showing properly

by JimJx (Beadle)
on Jun 13, 2003 at 04:33 UTC ( [id://265581]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that generates a page of identical images and when writing out the page, assigns a unique ID to each graphic based on a flat file. All of that works fine except for one thing..... Some of the ID fields will be empty. There doesn't seem to be any pattern to it as to length, specific ones, etc.

The flat file looks kinda like this:
FileName:GraphicName:Keywords
Example; AceSpades:Ace of Spades:Ace, Spades, Black, High card.......

In this instance, I am only using FileName. If I haven't completely confused everyone, any suggestions are appreciated. :-) If I have confused everyone, drop me a note and I will try to be more clear......

An example of the output is:

<DIV CLASS="Location1_1"> <IMG SRC="../tiles/thumbs/backside.jpg" id="Dagaz" onClick="trackClick +s(this)"> </DIV> <DIV CLASS="Location1_2"> <IMG SRC="../tiles/thumbs/backside.jpg" id="" onClick="trackClicks(thi +s)"> </DIV>
The first example is what should be displayed, the second is what I sometimes get.... The script is below.

#!e:\perl\perl.exe use CGI; use warnings; use Tie::File; use List::Util qw(shuffle); $query = CGI::new(); $Spread = $query->param("Spread"); $i=0; @place2 = qw(0 1_1 1_2 1_3 1_4 1_5 1_6 2_1 2_2 2_3 2_4 2_5 2_6 3_1 3_2 + 3_3 3_4 3_5 3_6 4_1 4_2 4_3 4_4 4_5 4_6); open(FILE,"< tiles.txt") or die "error opening tiles.txt $!"; tie @text, 'Tie::File', 'tiles.txt' or die $!; @text = shuffle(@text); while (<FILE>) { ($File[$i++], $Name[$i++], $Keywords[$i++]) = split(/:/; chomp ($Keywords[$i]); } close(FILE); print "content-type:text/html\n\n"; print "<HTML>\n<HEAD>\n"; print "<style type=\"text/css\">\n<!--\n"; print "\@import url(\"../styles.css\");\n//-->\n"; print "</style>\n\n"; print "<script type=\"text/javascript\">\n"; print "var max = $Count;\n"; print "function trackClicks(img) {\n"; print "self.c = self.c?c:0;\n"; print "self.cache = self.cache?cache:[];\n"; print "var found = false;\n"; print "for (var x = 0; x < cache.length; x++) {\n"; print "if (cache[x] == img.id) found = true;\n"; print "}\n"; print "if (!found) {\n"; print "cache.push(img.id);\n"; print "c++;\n"; print "}\n"; print "if (c >= max) {\n"; print "alert(cache)\n"; print "document.Selections.Cache.value=cache\n"; print "document.forms[0].submit();\n"; print "}\n"; print "}\n"; print "</script>\n"; print "</HEAD>\n"; print "<body>\n"; print "<DIV CLASS=\"MainWindow\">\n"; print "Test Layout<P>\n"; print "<DIV CLASS=\"MainContent\">\n"; print "<FORM NAME=\"Selections\" action=\"ShowLayout.pl\" method=\"POS +T\">\n"; for ($z=1; $z<=24; $z++) { print "<DIV CLASS=\"Location$place2[$z]\">\n"; print "<IMG SRC=\"../tiles/thumbs/backside.jpg\" id=\"$File[$z]\" onCl +ick=\"trackClicks(this)\">\n"; print "</DIV>\n"; } print "<Input type=\"hidden\" Name=\"Stones\" Value=\"$Stones\">\n"; print "<Input type=\"hidden\" Name=\"Cache\" Value=\"cache\">\n"; print "</FORM>\n";

Replies are listed 'Best First'.
Re: Code not showing properly
by kabel (Chaplain) on Jun 13, 2003 at 04:48 UTC
    i cannot get any meaning out of the shuffle over the tied array. in the while, you put everything in three arrays, so the shuffle is pointless.

    HOOPS, overlooked a bogus statement as well: ($File[$i++], $Name[$i++], $Keywords[$i++]) = split(/:/); first, the closing parenthesis is missing. second, you increase $i by every loop by 3! instead, move the incrementing out of it:
    ($File[$i], $Name[$i], $Keywords[$i]) = split(/:/); $i ++;


    you hardcoded that the arrays will have 24 elemenets. i do not think that is what you wanted. plus, arrays begin counting by zero. consider:
    for ($z=0; $z<scalar (@File); $z++) { ... }

    you can easily separate html markup from perl code with HTML::Template.

    HTH
      Hi Kabel,

      Thanks for the reply!
      i cannot get any meaning out of the shuffle over the tied array. in the while, you put everything in three arrays, so the shuffle is pointless.
      Actually, not pointless. I shuffled the whole file before reading the information into the arrays, that is all. I do not want each field shuffled individually.... Unless I completely misunderstood you which is possible. :-)

      first, the closing parenthesis is missing. second, you increase $i by every loop by 3! instead, move the incrementing out of it
      I caught both of those right after I made the post..... Don't know what I was thinking at the time, but both are fixed.

      you hardcoded that the arrays will have 24 elemenets. i do not think that is what you wanted. plus, arrays begin counting by zero
      In the first iteration of this, yes the arrays will have 24 elements.... After this page executes, they get cut down to between 1 and 9, depending on choices made. As far as the arrays starting with 0, for some reason I have always used 1 for the beginning. But that doesn't change anything except the number assigned. It doesn't change the fact that I still wind up with empty IDs in the code when the page renders.

      Jim

Re: Code not showing properly
by chromatic (Archbishop) on Jun 13, 2003 at 04:35 UTC

    You're incrementing $i three times in the line with the split. That makes for many holes in your three arrays. Perhaps a push would be easier. It's a bit longer, though -- I might keep an array of hashes instead.

    while (<FILE>) { chomp; my ($file, $name, $keyword) = split /:/, $_; push @File, $file; push @Name, $name; push @Keywords, $keyword; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://265581]
Approved by vek
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found