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

I'm trying to get the $n variable (pagecount from MySQL) to carry to the array below it.
Something in my syntax isn't working (and I'm not seeing it!). Any hints greatly appreciated.
my $pagecount = $total_recs / 10; my $n = $pagecount; print ("Page 1 of "); print POSIX::ceil($n), $/; my @pages = (1 .. $n); for $i (@pages) { print "<a href=\"http://www.example.com/page$i.shtml\">http:// +www.example.com/page$i.shtml</a><br>\n"; }
Many thanks in advance,

-Bob

Replies are listed 'Best First'.
Re: using pagecount variable in array?
by liverpole (Monsignor) on Jul 23, 2006 at 14:10 UTC
    Hi bobafifi,

    Something in my syntax isn't working (and I'm not seeing it!)

    We're not seeing it either, if you don't show us!  What exactly ARE you getting?  When I added:

    use strict; use warnings;

    at the top of the program, and assigned a value to $total_recs (since I have no idea what value you are starting with):

    my $total_recs = 123;

    and changed for $i (@pages) to for my $i (@pages), I got the output:

    Page 1 of <a href="http://www.example.com/page1.shtml">http://www.exam +ple.com/pa ge1.shtml</a><br> <a href="http://www.example.com/page2.shtml">http://www.example.com/pa +ge2.shtml< /a><br> <a href="http://www.example.com/page3.shtml">http://www.example.com/pa +ge3.shtml< /a><br> <a href="http://www.example.com/page4.shtml">http://www.example.com/pa +ge4.shtml< /a><br> <a href="http://www.example.com/page5.shtml">http://www.example.com/pa +ge5.shtml< /a><br> <a href="http://www.example.com/page6.shtml">http://www.example.com/pa +ge6.shtml< /a><br> <a href="http://www.example.com/page7.shtml">http://www.example.com/pa +ge7.shtml< /a><br> <a href="http://www.example.com/page8.shtml">http://www.example.com/pa +ge8.shtml< /a><br> <a href="http://www.example.com/page9.shtml">http://www.example.com/pa +ge9.shtml< /a><br> <a href="http://www.example.com/page10.shtml">http://www.example.com/p +age10.shtm l</a><br> <a href="http://www.example.com/page11.shtml">http://www.example.com/p +age11.shtm l</a><br> <a href="http://www.example.com/page12.shtml">http://www.example.com/p +age12.shtm l</a><br>

    which looks fine to me.  It's the output for each page from 1 to 12 (as 123 / 10 = 12.3).

    Please explain exactly what YOU are getting, and why you think it's wrong.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Wow! That worked :-)

      Changed for $i (@pages) to for my $i (@pages) and that did the trick.

      You rock!! :-)
      Thanks so much,

      -Bob

        You're mistaking the cause for your error. In fact, the code you posted will run AS-IS if you simply assign a value to $total_recs at the start of the script, and use POSIX qw/ceil/;. Though it's wise beyond words to learn to use strict;, and smart to use lexical variables such as my $i, the change you made by adding my $i to your foreach loop can't be what fixed the code you posted. The code you posted essentially works.

        So what exactly was the error you were getting?


        Dave

        Did you also change the top of your script to include use strict;? I think that was much of the point of liverpole's post.

            -Bryan